<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<meta name="theme-color" content="#222">

<script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






  <script>
  (function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/6f697dad.js","daovoice")
  daovoice('init', {
      app_id: "6f697dad"
    });
  daovoice('update');
  </script>















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="huangguangda,达叔,达叔小生,广东机电职业技术学院,达叔和他的朋友们,Android,Java,小可爱">





  <link rel="alternate" href="/atom.xml" title="达叔小生" type="application/atom+xml">






<meta name="description" content="用一切抱怨的时间去努力">
<meta property="og:type" content="website">
<meta property="og:title" content="达叔小生">
<meta property="og:url" content="https://huangguangda.github.io/page/8/index.html">
<meta property="og:site_name" content="达叔小生">
<meta property="og:description" content="用一切抱怨的时间去努力">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="达叔小生">
<meta name="twitter:description" content="用一切抱怨的时间去努力">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://huangguangda.github.io/page/8/">




<!-- 网页加载条 -->
<script src="https://neveryu.github.io/js/src/pace.min.js"></script>
  <title>达叔小生</title>
  









  <!-- 音频播放 -->
<link rel="stylesheet" href="/dist/APlayer.min.css">
<div id="aplayer"></div>
<script type="text/javascript" src="/dist/APlayer.min.js"></script>
<script type="text/javascript" src="/dist/music.js"></script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-home">
    <div class="headband"></div>

    <a href="https://github.com/huangguangda" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">达叔小生</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">爱好分享的码农</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-首页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-关于">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-标签">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-分类">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-归档">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-书籍">
          <a href="/books" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br>
            
            书籍
          </a>
        </li>
      
        
        <li class="menu-item menu-item-电影">
          <a href="/movies" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-chrome"></i> <br>
            
            电影
          </a>
        </li>
      
        
        <li class="menu-item menu-item-站点">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br>
            
            站点
          </a>
        </li>
      
        
        <li class="menu-item menu-item-公益">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br>
            
            公益
          </a>
        </li>
      
        
        <li class="menu-item menu-item-课本">
          <a href="/book/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-apple"></i> <br>
            
            课本
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/19/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/19/1/" itemprop="url">Java面试集合（二）</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-19T15:18:05+08:00">
                2018-03-19
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java面试集合/" itemprop="url" rel="index">
                    <span itemprop="name">Java面试集合</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,085
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>Java面试集合（二）</code>的概述，希望你们喜欢</p>
<h1 id="二"><a href="#二" class="headerlink" title="二"></a>二</h1><p>1.请问线程有哪些状态？</p>
<blockquote>
<p>新建状态（New）<br>就绪状态（Runnable）<br>运行状态（Running）<br>阻塞状态（Blocked）<br>死亡状态（Terminated）</p>
</blockquote>
<p>2.表达线程代码？<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">new Thread()&#123;</span><br><span class="line"> public void run()&#123;&#125;</span><br><span class="line">&#125;.start();</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">new Thread(new Runnable()&#123;</span><br><span class="line"> public void run()&#123;&#125;</span><br><span class="line">&#125;).start();</span><br></pre></td></tr></table></figure>
<p>3.如何表示什么是接口？<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">interface Student&#123;</span><br><span class="line"> public void read();</span><br><span class="line"> public void write();</span><br><span class="line">&#125;</span><br><span class="line">class ChineseStudent implements Student&#123;</span><br><span class="line"> public void read()&#123;</span><br><span class="line"> System.out.println(&quot;read&quot;);</span><br><span class="line"> public void write()&#123;</span><br><span class="line"> System.out.println(&quot;write&quot;);</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">//测试类</span><br><span class="line">class Test&#123;</span><br><span class="line"> public static void main(String args[])&#123;</span><br><span class="line">  ChineseStudent chinesestudent = new ChineseStudent();</span><br><span class="line">  Student student = chinesestudent;</span><br><span class="line">  </span><br><span class="line">  student.read();</span><br><span class="line">  student.write();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">//implements关键字，继承多个接口</span><br><span class="line">interface Student&#123;</span><br><span class="line"> public void read();</span><br><span class="line"> public void write();</span><br><span class="line">&#125;</span><br><span class="line">interface Teacher&#123;</span><br><span class="line"> public void teach();</span><br><span class="line"> public void test();</span><br><span class="line">&#125;</span><br><span class="line">class Person implements Student,Teacher&#123;</span><br><span class="line">  public void read()&#123;</span><br><span class="line">   system.out.printlln(&quot;read&quot;);</span><br><span class="line">&#125;</span><br><span class="line">  public void write()&#123;</span><br><span class="line">   System.out.println(&quot;write&quot;);</span><br><span class="line">&#125;</span><br><span class="line">  public void teach()&#123;</span><br><span class="line">   System.out.println(&quot;teach&quot;);</span><br><span class="line">&#125;</span><br><span class="line">  public void test()&#123;</span><br><span class="line">   System.out.println(&quot;test&quot;);</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">//测试类</span><br><span class="line">class Test&#123;</span><br><span class="line"> public static void main(String args[])&#123;</span><br><span class="line">  Person person = new Person();</span><br><span class="line">  Student student = person;</span><br><span class="line">   student.read();</span><br><span class="line">   student.write();</span><br><span class="line">  Teacher teacher = person;</span><br><span class="line">    teacher.teach();</span><br><span class="line">    teacher.close();</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>4.简单说说String类和StringBuffer类之间的区别？<br>答：String类是不可变的类，字符串一旦被初始化就不可能改变；StringBuffer是可变的字符串类，可以修改字符串的值。</p>
<p>5.简单说说List，Set，Map的区别？</p>
<blockquote>
<p>List的特点是元素有序，元素可重复；List常见的实现类为ArrayList和LinkedList；Set的特点是元素的无序，元素不可重复，Set常见的实现类有HashSet和TreeSet；Map的特点是存储的元素是键（key）和值（Value）的映射关系，元素都是成对出现的，Map的常见实现类是HashMap和TreeMap。</p>
</blockquote>
<p>6.来描述类集合框架？<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">public class Test&#123;</span><br><span class="line"> public static void main(String[] args)&#123; </span><br><span class="line">  ArrayList list = new Arraylist();</span><br><span class="line">  for(int i = 0; i&lt;10; i++)&#123;</span><br><span class="line">   list.add(&quot;Test:&quot;+i);</span><br><span class="line">  &#125;</span><br><span class="line">  //打印</span><br><span class="line">  Iterator iterator = list.iterator();</span><br><span class="line">  while(iterator.hasNext())&#123;</span><br><span class="line">   //集合不能记住元素的类型</span><br><span class="line">   Object object = iterator.next();</span><br><span class="line">   System.out.println(object);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>7.说说字节流和字符流？<br>答：字节流的两个基类分别是InputStream和OutputStream，字符流的两个基类分别是Reader和Writer。字节流是以8位字节为单位的字节流类，而字符流是以16位字节为单位。</p>
<p>8.用代码介绍FileInputStream和FileOutputStream，以及BufferedReader和BufferedWriter？<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">public class Test&#123;</span><br><span class="line"> public static void main(String args) throws Exception&#123;</span><br><span class="line">  //字节流</span><br><span class="line"> FileInputStream in = new FileInputStream(&quot;C:/test.txt&quot;);</span><br><span class="line"> FileOutStream out = new FileOutputStream(&quot;D:/testone.txt&quot;);</span><br><span class="line">  byte[] buf = new byte[1024];</span><br><span class="line">  int len;</span><br><span class="line">  while( (len = in.read(buf))  !=  -1)&#123;</span><br><span class="line">   out.write(buf，0，len);</span><br><span class="line"> &#125;</span><br><span class="line"> in.close();</span><br><span class="line"> out.close();</span><br><span class="line"></span><br><span class="line">//字符流</span><br><span class="line">BufferedReader bf = new BufferedReader(new FileReadere(&quot;C:/test.txt&quot;));</span><br><span class="line">BufferedWriter bw = new BufferedWriter(new FileWriter(&quot;D://testtwo.txt&quot;));</span><br><span class="line">String str;</span><br><span class="line"> while( (str = bf.readLine() ) != null )&#123;</span><br><span class="line">  bw.write(str);</span><br><span class="line">  bw.newLine();</span><br><span class="line">&#125;</span><br><span class="line">bf.close();</span><br><span class="line">bw.close();</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>9.网络开发Socket和ServerSocket的表达？<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">//服务端</span><br><span class="line">public class ServerSocket1&#123;</span><br><span class="line"> public static void main(String[] args)&#123;</span><br><span class="line">  try&#123;</span><br><span class="line">   ServerSocket ss = new ServerSocket(2008);</span><br><span class="line">  while(true)&#123;</span><br><span class="line">  Socket s = ss.accept();</span><br><span class="line">  InputStream is = s.getInputStream();</span><br><span class="line">  OutputStream os = s.getOutputStream();</span><br><span class="line">  PrintStream ps = new PrintStream(os);</span><br><span class="line"> ps.println(&quot;hello, i am server&quot;);</span><br><span class="line"> </span><br><span class="line"> DataInputStream dis = new DataInputStream(is);</span><br><span class="line"> String str = dis.readLine();</span><br><span class="line"> System.out.println(str);</span><br><span class="line"> s.close();</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">catch(IOException ee)&#123;</span><br><span class="line"> System.out.println(ee);</span><br><span class="line">&#125;</span><br><span class="line">catch(Excepiton e)&#123;</span><br><span class="line"> System.out.println(e);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">//客户端</span><br><span class="line">public class ClientSocket&#123;</span><br><span class="line"> public static void main(String[] args)&#123;</span><br><span class="line"> try&#123;</span><br><span class="line"> Socket s  = new Socket(&quot;####id&quot;,2008);</span><br><span class="line"> InputStream is = s.getInputStream();</span><br><span class="line"> OutputStream os = s.getOutputStream();</span><br><span class="line"> PrintStream ps = new PrintStream(os);</span><br><span class="line"> ps.println(&quot;hello, i am client&quot;);</span><br><span class="line"> DataInputStream dis = new DataInputStream(is);</span><br><span class="line"> String str = dis.readLine();</span><br><span class="line"> System.out.println(str);</span><br><span class="line"> s.close();</span><br><span class="line">&#125;</span><br><span class="line">catch(ConnectException eee) &#123;</span><br><span class="line">     System.out.println(eee);</span><br><span class="line">   &#125;</span><br><span class="line">   catch(IOException ee) &#123;</span><br><span class="line">     System.out.println(ee);</span><br><span class="line">   &#125;</span><br><span class="line">   catch(Exception e) &#123;</span><br><span class="line">     System.out.println(e);</span><br><span class="line">   &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>10.谈谈，解惑？<br>答：对于我来说，我认为程序员并不是最好的职业，这是从享受生活的角度上看的，我听说过太多程序员的熬夜现象了，这是一门学到老的专业方向，如今IT的发展太快了，并且太多细节需要我们深入了解，这就大大让IT工作太累了，如果不是喜欢，不是兴趣，建议自己重新思考，重新定义。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了Java面试集合（二），如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/18/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/18/1/" itemprop="url">Android-如何显示版本号并制作3秒跳转页</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-18T15:31:31+08:00">
                2018-03-18
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android-Studio实战练习V2/" itemprop="url" rel="index">
                    <span itemprop="name">Android Studio实战练习V2</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  289
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>Android-如何显示版本号并制作3秒跳转页</code>的概述，希望你们喜欢</p>
<h2 id="创建布局文件"><a href="#创建布局文件" class="headerlink" title="创建布局文件"></a>创建布局文件</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;RelativeLayout</span><br><span class="line">xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">android:layout_height=&quot;match_parent&quot;</span><br><span class="line">android:layout_width=&quot;match_parent&quot;</span><br><span class="line">android:background=&quot;@drawable/launch_bg&quot;&gt;</span><br><span class="line">&lt;TextView</span><br><span class="line">android:id=&quot;@+id/tv_version&quot;</span><br><span class="line">android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">android:layout_centerInParent=&quot;true&quot;</span><br><span class="line">android:textColor=&quot;@android:color/white&quot;</span><br><span class="line">android:textSize=&quot;14sp&quot;/&gt;</span><br><span class="line">&lt;RelativeLayout&gt;</span><br></pre></td></tr></table></figure>
<h2 id="创建一个版本Activity类"><a href="#创建一个版本Activity类" class="headerlink" title="创建一个版本Activity类"></a>创建一个版本Activity类</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">public class SplashActivity extends AppCompatActivity&#123;</span><br><span class="line">private TextView textView;</span><br><span class="line">@Override</span><br><span class="line">protected void onCreate(Bundle savedInstanceState)&#123;</span><br><span class="line"> super.onCreate(savedInstanceState);</span><br><span class="line"> setContentView(R.layout.activity_splash);</span><br><span class="line"> </span><br><span class="line"> setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);</span><br><span class="line">  textView = findViewById(R.id.tv_version);</span><br><span class="line"> </span><br><span class="line"> try&#123;</span><br><span class="line">   PackageInfo info = getPackageManager().getPackageInfo(getPackageName(),0);</span><br><span class="line">   textView.setText(&quot;version:&quot;+info.versionName);</span><br><span class="line"> &#125;catch(PackageManger.NameNotFoundException e)&#123;</span><br><span class="line">   e.printStackTrace();</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> Timer timer = new Timer();</span><br><span class="line"> TimerTask timerTask = new TimeTask()&#123;</span><br><span class="line">  @Override</span><br><span class="line">  public void run()&#123;</span><br><span class="line">   Intent intent = new Intent(SplashActivity.this,MainActivity.class);</span><br><span class="line">   startActivity(intent);</span><br><span class="line">   SplashActivity.this.finish();</span><br><span class="line">   &#125;</span><br><span class="line"> &#125;;</span><br><span class="line"> timer.schedule(timerTask,3000);</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="在清单文件中，添加声明即可。"><a href="#在清单文件中，添加声明即可。" class="headerlink" title="在清单文件中，添加声明即可。"></a>在清单文件中，添加声明即可。</h3><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了Android-如何显示版本号并制作3秒跳转页，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/17/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/17/1/" itemprop="url">Android开发工程师文集-1 小时学会各种Drawable</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-17T15:01:46+08:00">
                2018-03-17
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android开发工程师文集/" itemprop="url" rel="index">
                    <span itemprop="name">Android开发工程师文集</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  735
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>Android开发工程师文集-1 小时学会各种Drawable</code>的概述，希望你们喜欢</p>
<h2 id="Drawable介绍"><a href="#Drawable介绍" class="headerlink" title="Drawable介绍"></a>Drawable介绍</h2><p>Drawable为抽象类，drawable与view有区别的地方，主要是体现事件和交互性上面。</p>
<p>view是面向用户的，是可见的控件，能给它添加点击事件。drawable则相反，不能面向，也不可见，也不能添加点击事件。</p>
<p>drawable有好多子类，drawable为抽象类，所以有很多子类来继承它。</p>
<h2 id="主要原理方法"><a href="#主要原理方法" class="headerlink" title="主要原理方法"></a>主要原理方法</h2><ul>
<li>draw()</li>
<li>setBounds()</li>
</ul>
<h2 id="BitmapDrawable-巴特美drawable"><a href="#BitmapDrawable-巴特美drawable" class="headerlink" title="BitmapDrawable 巴特美drawable"></a>BitmapDrawable 巴特美drawable</h2><p>该子类为bitmap的包装，可以保持原图片大小，填充，拉伸。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;bitmap</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line"> android:src=&quot;@drawable/ic_launcher&quot;</span><br><span class="line"> android:tileMode=&quot;mirror&quot;//平铺</span><br><span class="line"> android:antialias=&quot;true&quot;//消除锯齿</span><br><span class="line"> android:dither=&quot;true&quot;&gt;</span><br><span class="line">&lt;/bitmap&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="LayerDrawable-勒儿drawable"><a href="#LayerDrawable-勒儿drawable" class="headerlink" title="LayerDrawable 勒儿drawable"></a>LayerDrawable 勒儿drawable</h2><p>该子类就是来管理一组drawable的，第一张图片的放置，那么第二张图片会在第一张图片上，由此依次放置。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;</span><br><span class="line"> &lt;item</span><br><span class="line"> android:top=&quot;40dp&quot;</span><br><span class="line"> android:left=&quot;40dp&quot;</span><br><span class="line"> android:src=&quot;@drawable/picture_1&quot;</span><br><span class="line"> android:gravity=&quot;center&quot;&gt;</span><br><span class="line"> &lt;/item&gt;</span><br><span class="line"> &lt;item</span><br><span class="line"> android:top=&quot;40dp&quot;</span><br><span class="line"> android:left=&quot;40dp&quot;</span><br><span class="line"> android:src=&quot;@drawable/picture_2&quot;</span><br><span class="line"> android:gravity=&quot;center&quot;&gt;</span><br><span class="line"> &lt;/item&gt;</span><br><span class="line">&lt;/layer-list&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="StateListDrawable"><a href="#StateListDrawable" class="headerlink" title="StateListDrawable"></a>StateListDrawable</h2><p>该类StateListDrawable根据不同状态提供不同图片。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utr-8&quot;?&gt;</span><br><span class="line">&lt;selector</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;</span><br><span class="line"> &lt;item android:state_focused=&quot;true&quot; android:drawable=&quot;@drawable/botton_1&quot;/&gt;</span><br><span class="line"> &lt;item android:state_pressed=&quot;true&quot; android:drawable=&quot;@drawable/botton_2&quot;/&gt;</span><br><span class="line"> &lt;item android:state_selected=&quot;true&quot; android=&quot;@drawable/botton_3&quot;/&gt;</span><br><span class="line"> &lt;item android:drawable=&quot;@drawable/botton_4&quot;/&gt;</span><br><span class="line">&lt;/selector&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="LevelListDrawable"><a href="#LevelListDrawable" class="headerlink" title="LevelListDrawable"></a>LevelListDrawable</h2><p>该子类有不同的item -setlevel();<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;level-list</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.om/apk/res/android&quot;&gt;</span><br><span class="line"> &lt;item</span><br><span class="line">  android:drawable=&quot;@drawable/off&quot;</span><br><span class="line">  android:minLevel=&quot;5&quot;</span><br><span class="line">  android:maxLevel=&quot;10&quot;&gt;</span><br><span class="line"> &lt;/item&gt;</span><br><span class="line"> &lt;item</span><br><span class="line">  android:drawable=&quot;@drawable/on&quot;</span><br><span class="line">  android:minLevel=&quot;10&quot;</span><br><span class="line">  android:maxLevel=&quot;20&quot;&gt;</span><br><span class="line"> &lt;/item&gt;</span><br><span class="line">&lt;/level-list&gt;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public void On(View v)&#123;</span><br><span class="line"> iv.setImageLevel( //int );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="TransitionDrawable-—-transi-tion-drawable"><a href="#TransitionDrawable-—-transi-tion-drawable" class="headerlink" title="TransitionDrawable — transi tion drawable"></a>TransitionDrawable — transi tion drawable</h2><p>为LayerDrawable的子类，只有两张图片的变化，没有多个图片，LayerDrawable可以有多个item，而transitiondrawable只有两个item。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;</span><br><span class="line"> &lt;item android:drawable=&quot;@drawable/off&quot;&gt;</span><br><span class="line"> &lt;/item&gt;</span><br><span class="line"> &lt;item android:drawable=&quot;@drawable/on&quot;&gt;</span><br><span class="line"> &lt;/item&gt;</span><br><span class="line">&lt;/transition&gt;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">public void On(View v)&#123;</span><br><span class="line"> TransitionDrawable drawable=iv.getDrawable();</span><br><span class="line"> //开启 startTransition();</span><br><span class="line"> drawable.startTransition(2000);</span><br><span class="line">&#125;</span><br><span class="line">public void Off(View v)&#123;</span><br><span class="line"> TranstionDrawable drawable=iv.getDrawable();</span><br><span class="line"> //关闭 reverseTransition();</span><br><span class="line"> drawable.reverseTransition(2000);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="InsertDrawable"><a href="#InsertDrawable" class="headerlink" title="InsertDrawable"></a>InsertDrawable</h2><p>InsertDrawable表示嵌入图片==padding<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;inset </span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line"> android:drawable=&quot;@drawable/background&quot;</span><br><span class="line"> android:insetLeft=&quot;50dp&quot;</span><br><span class="line"> android:insetRight=&quot;50dp&quot;</span><br><span class="line"> android:insetTop=&quot;50dp&quot;</span><br><span class="line"> android:insetBottom=&quot;50dp&quot;&gt;</span><br><span class="line">&lt;/inset&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="ClipDrawable-可累drawable"><a href="#ClipDrawable-可累drawable" class="headerlink" title="ClipDrawable 可累drawable"></a>ClipDrawable 可累drawable</h2><p>该ClipDrawabel是一种裁剪的形式，如同进度条。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;clip xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line"> android:drawable=&quot;@drawable/picture&quot;</span><br><span class="line"> android:clipOrientation=&quot;horizontal&quot;</span><br><span class="line"> android:gravity=&quot;left&quot;/&gt;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ClipDrawable drawable=iv.getDrawable();</span><br><span class="line">drawable.setLevel(5000);</span><br><span class="line">//范围0-10000；</span><br></pre></td></tr></table></figure>
<h2 id="DIY的Drawable"><a href="#DIY的Drawable" class="headerlink" title="DIY的Drawable"></a>DIY的Drawable</h2><p>自己动手试试吧<br>梳理：<br>BitmapDrawable,<br>LayerDrawable,<br>StateListDreawable,<br>LevelListDrawable,<br>TransitionDrawable,<br>InsertDrawable,<br>ClipDrawable.</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了Android开发工程师文集-1 小时学会各种Drawable，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/16/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/16/1/" itemprop="url">Android开发工程师文集-1 小时学会SQLite</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-16T15:03:52+08:00">
                2018-03-16
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android开发工程师文集/" itemprop="url" rel="index">
                    <span itemprop="name">Android开发工程师文集</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,717
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  16
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>Android开发工程师文集-1 小时学会SQLite</code>的概述，希望你们喜欢</p>
<h2 id="内容"><a href="#内容" class="headerlink" title="内容"></a>内容</h2><ul>
<li>什么是Sqlite：<br>效率高，开源，小型，程序驱动，支持事务操作，无数据类型，可嵌入的关系型数据库<br>独立的，跨平台的，代码量少，简单易用</li>
</ul>
<p><strong>创建表语句</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">create table student(_id Integer primary key, name varchar(10), age Integer not null);</span><br></pre></td></tr></table></figure></p>
<p><strong>删除表</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">drop table student;</span><br></pre></td></tr></table></figure></p>
<p><strong>插入数据</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Insert into 表名(字段列表) values (值列表);</span><br><span class="line">insert into student(_id,age) values(1,17);</span><br><span class="line">insert into student values(1,&quot;vic&quot;,17);</span><br></pre></td></tr></table></figure></p>
<p><strong>修改数据</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">update student set name=&quot;vic&quot;,age=17 where _id=1;</span><br></pre></td></tr></table></figure></p>
<p><strong>更新数据</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Update 表名 set 字段=值 列表 更新的条件</span><br></pre></td></tr></table></figure></p>
<p><strong>删除数据</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">delete from 表名 [删除条件];</span><br><span class="line">delete from student where _id=1;</span><br></pre></td></tr></table></figure></p>
<p><strong>查询语句</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">select 字段名 from 表名称 [查询条件];</span><br><span class="line">select 列名称 from 表名称 where 条件;</span><br><span class="line">group by 分组的字段 having 筛选条件 order by 排序字段;</span><br><span class="line">select * from student;</span><br><span class="line">select _id from student;</span><br><span class="line">select * from student where _id=1;</span><br><span class="line">select * from student where _id=1 and age&gt;17;</span><br><span class="line">select * from student where age like &quot;%1%&quot;;</span><br><span class="line">select * from student where age&gt;17 order by _id=1;</span><br></pre></td></tr></table></figure></p>
<h2 id="内容-1"><a href="#内容-1" class="headerlink" title="内容"></a>内容</h2><ul>
<li>创建数据库</li>
<li>实现数据库中的增删改查</li>
</ul>
<h2 id="要点"><a href="#要点" class="headerlink" title="要点"></a>要点</h2><p><code>SQLiteOpenHelper</code>,<code>onCreate()</code>,<code>onUpgrade()</code>,<code>onOpen()</code><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@drawable/login_bg&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line"> &lt;Button</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;创建&quot;</span><br><span class="line">  android:onClick=&quot;create&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">//MainActivity.java</span><br><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line">  private MySqliteHelper helper;</span><br><span class="line"> @Override</span><br><span class="line"> protected void onCreate(Bundle savedInstanceState)&#123;</span><br><span class="line"> super.onCreate(savedInstanceState);</span><br><span class="line"> setContentView(R.layout.activity_main);</span><br><span class="line"> helper = DbManger.getIntance(this);</span><br><span class="line"> &#125;</span><br><span class="line"> public void createdb(View view)&#123;</span><br><span class="line">  SQLiteDatabase db=helper.getWritableDatable();</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">//创建一个类</span><br><span class="line">//sqliteOpenHelper</span><br><span class="line">//提供了onCreate()和onUpgrade()与onOpen()</span><br><span class="line">public class MySqliteHelper extends SQLiteOpenHelper&#123;</span><br><span class="line"> public MySqliteHelper(Context context, String name, SQLitebase.CursorFactory factory, int version)&#123;</span><br><span class="line"> super(context,name,factory,version);</span><br><span class="line"> &#125;</span><br><span class="line"> public MySqliteHelper(Context context)&#123;</span><br><span class="line">  super(context,Constant.DATABASE_NAME,null,Constant.DATABASE_VERSION);</span><br><span class="line">&#125;</span><br><span class="line"> //数据库创建时回调</span><br><span class="line"> @Override</span><br><span class="line"> public void onCreate(SQLiteDatabase db)&#123;</span><br><span class="line">  Log.i(&quot;tag&quot;,&quot;--onCreate--&quot;);</span><br><span class="line">  //String sql=&quot;create table student(_id Integer primary key,name verchar(10),age Integer)&quot;;</span><br><span class="line">  String sql = &quot;create table &quot;+Constant.TABLE_NAME+&quot;(&quot;+Constant._ID+&quot; Integer primary key,&quot;+Constant.NAME+&quot; varchar(10),&quot;+Constant.AGE+&quot; Integer)&quot;;</span><br><span class="line">  db.execSQL(sql);//执行数据库语句</span><br><span class="line"> &#125;</span><br><span class="line"> //数据库更新</span><br><span class="line"> @Override</span><br><span class="line"> public void onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion)&#123;</span><br><span class="line">  Log.i(&quot;tag&quot;,&quot;--onUpgrade--&quot;);</span><br><span class="line"> &#125;</span><br><span class="line"> //数据库打开</span><br><span class="line"> @Override</span><br><span class="line"> public void onOpen(SQLiteDatabase db)&#123;</span><br><span class="line">  super.onOpen(db);</span><br><span class="line">  Log.i(&quot;tag&quot;,&quot;--onOpen--&quot;);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">//创建库表</span><br><span class="line">public class Contant&#123;</span><br><span class="line"> public static final String DATABASE_NAME=“info.db”;//数据库名称</span><br><span class="line"> public static final int DATABASE_VERSION=1;//数据库的版本号</span><br><span class="line"> public static final String TABLE_NAME=&quot;student&quot;;//表名</span><br><span class="line">  //用这里表示</span><br><span class="line"> public static final String _ID=&quot;_id&quot;;</span><br><span class="line"> public static final String _NAME=&quot;name&quot;;</span><br><span class="line"> public static final String AGE=&quot;age&quot;;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public class DbManger&#123;</span><br><span class="line"> private static MySqliteHelper helper;</span><br><span class="line"> public static MySqliteHelper getIntance(Context context)&#123;</span><br><span class="line">   if(helper == null)&#123;</span><br><span class="line">     helper=new MySqliteHelper(content);</span><br><span class="line">   &#125;</span><br><span class="line">   return hepler;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@drawable/login_bg&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line"> &lt;Button</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;创建&quot;</span><br><span class="line">  android:onClick=&quot;create&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_insert&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;插入数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">//MainActivity.java</span><br><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line">  private MySqliteHelper helper;</span><br><span class="line"> @Override</span><br><span class="line"> protected void onCreate(Bundle savedInstanceState)&#123;</span><br><span class="line"> super.onCreate(savedInstanceState);</span><br><span class="line"> setContentView(R.layout.activity_main);</span><br><span class="line"> helper = DbManger.getIntance(this);</span><br><span class="line"> &#125;</span><br><span class="line"> public void createdb(View view)&#123;</span><br><span class="line">  SQLiteDatabase db=helper.getWritableDatable();</span><br><span class="line"> &#125;</span><br><span class="line"> public void click(View view)&#123;</span><br><span class="line">  switch(view.getId())&#123;</span><br><span class="line">   case R.id.btn_insert:</span><br><span class="line">    SQLiteDatabase db=helper.getWritableDatabase();</span><br><span class="line">    String sql=&quot;insert into &quot;+Constant.TABLE_NAME+&quot; values(1,&apos;vic&apos;,17)&quot;;</span><br><span class="line">    DbManger.execSQL(db,sql);</span><br><span class="line">    String sql2=&quot;insert into &quot;+Constant.TABLE_NAME+&quot; values(2,&apos;vic&apos;,23)&quot;;</span><br><span class="line">    DbManger.execSQL(db,sql2);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">public class DbManger&#123;</span><br><span class="line"> private static MySqliteHelper helper;</span><br><span class="line"> public static MySqliteHelper getIntance(Context context)&#123;</span><br><span class="line">   if(helper == null)&#123;</span><br><span class="line">     helper=new MySqliteHelper(content);</span><br><span class="line">   &#125;</span><br><span class="line">   return hepler;</span><br><span class="line"> &#125;</span><br><span class="line"> public static void execSQL(SQLiteDatabase db,String sql)&#123;</span><br><span class="line">  if(db!=null)&#123;</span><br><span class="line">   if(sql!=null &amp;&amp; !&quot;&quot;.equals(sql))&#123;</span><br><span class="line">     db.execSQL(sql);</span><br><span class="line">  &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@drawable/login_bg&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line"> &lt;Button</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;创建&quot;</span><br><span class="line">  android:onClick=&quot;create&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_insert&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;插入数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_update&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;修改数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">//MainActivity.java</span><br><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line">  private MySqliteHelper helper;</span><br><span class="line"> @Override</span><br><span class="line"> protected void onCreate(Bundle savedInstanceState)&#123;</span><br><span class="line"> super.onCreate(savedInstanceState);</span><br><span class="line"> setContentView(R.layout.activity_main);</span><br><span class="line"> helper = DbManger.getIntance(this);</span><br><span class="line"> &#125;</span><br><span class="line"> public void createdb(View view)&#123;</span><br><span class="line">  SQLiteDatabase db=helper.getWritableDatable();</span><br><span class="line"> &#125;</span><br><span class="line"> public void click(View view)&#123;</span><br><span class="line">  switch(view.getId())&#123;</span><br><span class="line">   case R.id.btn_insert:</span><br><span class="line">    SQLiteDatabase db=helper.getWritableDatabase();</span><br><span class="line">    String sql=&quot;insert into &quot;+Constant.TABLE_NAME+&quot; values(1,&apos;vic&apos;,17)&quot;;</span><br><span class="line">    DbManger.execSQL(db,sql);</span><br><span class="line">    String sql2=&quot;insert into &quot;+Constant.TABLE_NAME+&quot; values(2,&apos;vic&apos;,23)&quot;;</span><br><span class="line">    DbManger.execSQL(db,sql2);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   case R.id.btn_update:</span><br><span class="line">    db=helper.getWritableDatabase();</span><br><span class="line">    String updateSql=&quot;update &quot;+Constant.TABLE_NAME&quot;+&quot; set &quot;+Contant.NAME+&quot;=&apos;vic2&apos; where &quot;+Contant._ID+&quot;=1&quot;;</span><br><span class="line">    DbManger.execSQL(db,updateSql);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@drawable/login_bg&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line"> &lt;Button</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;创建&quot;</span><br><span class="line">  android:onClick=&quot;create&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_insert&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;插入数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_update&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;修改数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_delete&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;删除数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">//MainActivity.java</span><br><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line">  private MySqliteHelper helper;</span><br><span class="line"> @Override</span><br><span class="line"> protected void onCreate(Bundle savedInstanceState)&#123;</span><br><span class="line"> super.onCreate(savedInstanceState);</span><br><span class="line"> setContentView(R.layout.activity_main);</span><br><span class="line"> helper = DbManger.getIntance(this);</span><br><span class="line"> &#125;</span><br><span class="line"> public void createdb(View view)&#123;</span><br><span class="line">  SQLiteDatabase db=helper.getWritableDatable();</span><br><span class="line"> &#125;</span><br><span class="line"> public void click(View view)&#123;</span><br><span class="line">  switch(view.getId())&#123;</span><br><span class="line">   case R.id.btn_insert:</span><br><span class="line">    SQLiteDatabase db=helper.getWritableDatabase();</span><br><span class="line">    String sql=&quot;insert into &quot;+Constant.TABLE_NAME+&quot; values(1,&apos;vic&apos;,17)&quot;;</span><br><span class="line">    DbManger.execSQL(db,sql);</span><br><span class="line">    String sql2=&quot;insert into &quot;+Constant.TABLE_NAME+&quot; values(2,&apos;vic&apos;,23)&quot;;</span><br><span class="line">    DbManger.execSQL(db,sql2);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   case R.id.btn_update:</span><br><span class="line">    db=helper.getWritableDatabase();</span><br><span class="line">    String updateSql=&quot;update &quot;+Constant.TABLE_NAME&quot;+&quot; set &quot;+Contant.NAME+&quot;=&apos;vic2&apos; where &quot;+Contant._ID+&quot;=1&quot;;</span><br><span class="line">    DbManger.execSQL(db,updateSql);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   case R.id.btn_delete:</span><br><span class="line">    db=helper.getWritableDatabase();</span><br><span class="line">    String delSql=&quot;delete from &quot;+Constant.TABLE_NAME+&quot; where &quot;+Constant._ID+&quot;=2&quot;;</span><br><span class="line">    DbManger.execSQL(db,delSql);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line"> xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@drawable/login_bg&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line"> &lt;Button</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;创建&quot;</span><br><span class="line">  android:onClick=&quot;create&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_insert&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;插入数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_update&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;修改数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_delete&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;删除数据&quot;</span><br><span class="line">  android:onClick=&quot;click&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_insertApi&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;插入数据&quot;</span><br><span class="line">  android:onClick=&quot;onclick&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">//MainActivity.java</span><br><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line">  private MySqliteHelper helper;</span><br><span class="line"> @Override</span><br><span class="line"> protected void onCreate(Bundle savedInstanceState)&#123;</span><br><span class="line"> super.onCreate(savedInstanceState);</span><br><span class="line"> setContentView(R.layout.activity_main);</span><br><span class="line"> helper = DbManger.getIntance(this);</span><br><span class="line"> &#125;</span><br><span class="line"> public void createdb(View view)&#123;</span><br><span class="line">  SQLiteDatabase db=helper.getWritableDatable();</span><br><span class="line"> &#125;</span><br><span class="line"> public void onClick(View view)&#123;</span><br><span class="line">  switch(view.getId())&#123;</span><br><span class="line">   case R.id.btn_insertApi:</span><br><span class="line">    SQLiteDatabase db=helper.getWritableDatabase();</span><br><span class="line">    ContentValues values=new ContentValues();</span><br><span class="line">    values.put(Constant._ID,3);</span><br><span class="line">    values.put(Constant.NAME,&quot;vic&quot;);</span><br><span class="line">    values.put(Constant.AGE,17);</span><br><span class="line">    long result=db.insert(Constant.TABLE_NAME,null,values);</span><br><span class="line">    if(result&gt;0)&#123;</span><br><span class="line">     Toast.makeText(MainActivity.this,&quot;插入数据成功!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">      Toast.makeText(MainActivity.this,&quot;插入数据失败!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"> public void click(View view)&#123;</span><br><span class="line">  switch(view.getId())&#123;</span><br><span class="line">   case R.id.btn_insert:</span><br><span class="line">    SQLiteDatabase db=helper.getWritableDatabase();</span><br><span class="line">    String sql=&quot;insert into &quot;+Constant.TABLE_NAME+&quot; values(1,&apos;vic&apos;,17)&quot;;</span><br><span class="line">    DbManger.execSQL(db,sql);</span><br><span class="line">    String sql2=&quot;insert into &quot;+Constant.TABLE_NAME+&quot; values(2,&apos;vic&apos;,23)&quot;;</span><br><span class="line">    DbManger.execSQL(db,sql2);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   case R.id.btn_update:</span><br><span class="line">    db=helper.getWritableDatabase();</span><br><span class="line">    String updateSql=&quot;update &quot;+Constant.TABLE_NAME&quot;+&quot; set &quot;+Contant.NAME+&quot;=&apos;vic2&apos; where &quot;+Contant._ID+&quot;=1&quot;;</span><br><span class="line">    DbManger.execSQL(db,updateSql);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   case R.id.btn_delete:</span><br><span class="line">    db=helper.getWritableDatabase();</span><br><span class="line">    String delSql=&quot;delete from &quot;+Constant.TABLE_NAME+&quot; where &quot;+Constant._ID+&quot;=2&quot;;</span><br><span class="line">    DbManger.execSQL(db,delSql);</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_insertApi&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;插入数据&quot;</span><br><span class="line">  android:onClick=&quot;onclick&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_updateApi&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;更新数据&quot;</span><br><span class="line">  android:onClick=&quot;onclick&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">public void onClick(View view)&#123;</span><br><span class="line">  switch(view.getId())&#123;</span><br><span class="line">   case R.id.btn_insertApi:</span><br><span class="line">    SQLiteDatabase db=helper.getWritableDatabase();</span><br><span class="line">    ContentValues values=new ContentValues();</span><br><span class="line">    values.put(Constant._ID,3);</span><br><span class="line">    values.put(Constant.NAME,&quot;vic&quot;);</span><br><span class="line">    values.put(Constant.AGE,17);</span><br><span class="line">    long result=db.insert(Constant.TABLE_NAME,null,values);</span><br><span class="line">    if(result&gt;0)&#123;</span><br><span class="line">     Toast.makeText(MainActivity.this,&quot;插入数据成功!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">      Toast.makeText(MainActivity.this,&quot;插入数据失败!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   case R.id.btn_updateApi:</span><br><span class="line">    //String table 修改的数据表的名称,ContentValues values,String whereClause 表示修改条件,String[] whereArgs</span><br><span class="line">    db=helper.getWritableDatabase();</span><br><span class="line">    //db.update(String table,ContentValues values,String whereClause,String[] whereArgs);</span><br><span class="line">    ContentValues cv=new ContentValues();</span><br><span class="line">    cv.put(Contant.NAME,&quot;vic3&quot;);</span><br><span class="line">    int count=db.update(Constant.TABLE_NAME,cv,Contant.TABLE_NAME,cv,Constant._ID+&quot;=?&quot;,new String[]&#123;&quot;3&quot;&#125;);</span><br><span class="line">    if(count&gt;0)&#123;</span><br><span class="line">     Toast.makeText(MainActivity.this,&quot;插入数据成功!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">      Toast.makeText(MainActivity.this,&quot;插入数据失败!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;Button</span><br><span class="line">  android:id=&quot;@+id/btn_updateApi&quot;</span><br><span class="line">  android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">  android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">  android:text=&quot;更新数据&quot;</span><br><span class="line">  android:onClick=&quot;onclick&quot;</span><br><span class="line">  android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">  android:background=&quot;#000000&quot;/&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">public void onClick(View view)&#123;</span><br><span class="line">  switch(view.getId())&#123;</span><br><span class="line">   case R.id.btn_insertApi:</span><br><span class="line">    SQLiteDatabase db=helper.getWritableDatabase();</span><br><span class="line">    ContentValues values=new ContentValues();</span><br><span class="line">    values.put(Constant._ID,3);</span><br><span class="line">    values.put(Constant.NAME,&quot;vic&quot;);</span><br><span class="line">    values.put(Constant.AGE,17);</span><br><span class="line">    long result=db.insert(Constant.TABLE_NAME,null,values);</span><br><span class="line">    if(result&gt;0)&#123;</span><br><span class="line">     Toast.makeText(MainActivity.this,&quot;插入数据成功!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">      Toast.makeText(MainActivity.this,&quot;插入数据失败!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">   case R.id.btn_updateApi:</span><br><span class="line">    //String table 修改的数据表的名称,ContentValues values,String whereClause 表示修改条件,String[] whereArgs</span><br><span class="line">    db=helper.getWritableDatabase();</span><br><span class="line">    //db.update(String table,ContentValues values,String whereClause,String[] whereArgs);</span><br><span class="line">    ContentValues cv=new ContentValues();</span><br><span class="line">    cv.put(Contant.NAME,&quot;vic3&quot;);</span><br><span class="line">    int count=db.update(Constant.TABLE_NAME,cv,Contant.TABLE_NAME,cv,Constant._ID+&quot;=?&quot;,new String[]&#123;&quot;3&quot;&#125;);</span><br><span class="line">    if(count&gt;0)&#123;</span><br><span class="line">     Toast.makeText(MainActivity.this,&quot;插入数据成功!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">      Toast.makeText(MainActivity.this,&quot;插入数据失败!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;</span><br><span class="line">    db.close();</span><br><span class="line">    break;</span><br><span class="line">  case R.id.btn_deleteApi:</span><br><span class="line">  db=helper.getWritableDatabase();</span><br><span class="line"></span><br><span class="line">  //int count2=db.delete(String table,StringwhereClause,String[] whereArgs);</span><br><span class="line">  int count2=db.delete(Constant.TABLE_NAME,Constant._ID+&quot;=?&quot;,new String[]&#123;&quot;1&quot;&#125;);</span><br><span class="line">  if(count2&gt;0)&#123;</span><br><span class="line">     Toast.makeText(MainActivity.this,&quot;插入数据成功!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">      Toast.makeText(MainActivity.this,&quot;插入数据失败!&quot;,Toast.LENGTH_LONG).show();</span><br><span class="line">    &#125;</span><br><span class="line">  db.close();</span><br><span class="line">  break;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"> public int delete(String table,String whereClause,String[] whereArgs)&#123;</span><br><span class="line">  acquireReference();</span><br><span class="line">  try&#123;</span><br><span class="line">   SQLiteStatement statement = new SQLiteStatement(this,&quot;DELETE FROM &quot;+table+(!TextUtils.isEmpty(whereClause) ? &quot; WHERE &quot;+whereClause : &quot;&quot;), whereArgs;</span><br><span class="line"> try&#123;</span><br><span class="line">  return statement.executeUpdateDelete();</span><br><span class="line">  &#125;finally&#123;</span><br><span class="line">   statement.close();</span><br><span class="line">  &#125;</span><br><span class="line">  &#125;finally&#123;</span><br><span class="line">  releaseReference();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了Android开发工程师文集-1 小时学会SQLite，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/15/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/15/1/" itemprop="url">Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-15T15:13:35+08:00">
                2018-03-15
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/随笔/" itemprop="url" rel="index">
                    <span itemprop="name">随笔</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  386
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>Android开发的插件Code Generator与LayoutCreator的安装与使用，提升你的开发效率</code>的概述，希望你们喜欢</p>
<h2 id="学习目标"><a href="#学习目标" class="headerlink" title="学习目标"></a>学习目标</h2><p>掌握两个插件的安装和使用，能够实现代码生成功能。</p>
<p>Android Code Generator是一款代码生成的插件，帮助提高app的开发速度，只要打好布局XML文件，就能帮你把Activity/Fragment/Adapter文件生成好。</p>
<p>而LayoutCreator可以让你在Activity/Fragment中自动生成findViewById等布局相关初始化代码。</p>
<h2 id="Android-Studio安装插件的方法"><a href="#Android-Studio安装插件的方法" class="headerlink" title="Android Studio安装插件的方法"></a>Android Studio安装插件的方法</h2><p>点击File-&gt;Settings-&gt;plugins，在settings中的Plugins，通过Plugins下载安装，安装完成后要重新启动Android Studio。</p>
<p>Search in respositories/Browse respositories可以查找到本地没有安装的插件，选择想要安装的插件install，重启Android Studio即可完成安装插件过程，然后如果你还要继续下载就进行查询，最后重新启动Android Studio就行了。</p>
<h2 id="补充"><a href="#补充" class="headerlink" title="补充"></a>补充</h2><p>Code Generator只会帮你声明带id的控件，当XML中出现文字符时，会报错，我们可以先把encoding改成gbk格式，等生成activity后再把gbk改回utf-8就好了。</p>
<p>以上就是比较实用的几个插件，如果大家有更好的解决方法，欢迎讨论。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了Android开发的插件Code Generator与LayoutCreator的安装与使用，提升你的开发效率，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/14/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/14/1/" itemprop="url">Android精通教程-第一节Android入门简介</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-14T15:06:06+08:00">
                2018-03-14
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android精通教程/" itemprop="url" rel="index">
                    <span itemprop="name">Android精通教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  869
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  3
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>Android精通教程-第一节Android入门简介</code>的概述，希望你们喜欢</p>
<h2 id="每日一句"><a href="#每日一句" class="headerlink" title="每日一句"></a>每日一句</h2><p>If life were predictable it would cease to be life, and be without flavor.<br>— Eleanor Roosevelt<br><img src="https://upload-images.jianshu.io/upload_images/11158618-d4e7df48351852d6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="每日一句"></p>
<h2 id="入门工具的安装"><a href="#入门工具的安装" class="headerlink" title="入门工具的安装"></a>入门工具的安装</h2><p><strong>第一步：</strong>要下载的朋友注意了，先看完教程了解个大概，下载Android studio前，先安装jdk和IDE，Android SDK</p>
<p><strong>第二步：</strong>下载jdk，提供JDK 9 地址：<br><a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html" target="_blank" rel="noopener">http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html</a><br>提供JDK 8 地址：<br><a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" target="_blank" rel="noopener">http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html</a></p>
<p><strong>第三步：</strong>首先登录Android studio 官网地址<br><a href="https://developer.android.google.cn/studio/index.html" target="_blank" rel="noopener">https://developer.android.google.cn/studio/index.html</a><br>接下来我提供中文社区Android studio下载地址：<br><a href="http://tools.android-studio.org/index.php" target="_blank" rel="noopener">http://tools.android-studio.org/index.php</a></p>
<p>我以windows64位例子下载教程：<br>点击 android-studio-ide-171.4408382-windows.exe 无 Android SDK 进行下载<br>然后进行点击你所下载的软件，进行安装。</p>
<p><strong>第四步：</strong><br>配置环境变量JAVA_HOME，操作步骤：<br>“计算机（我的电脑）”—-“属性”—-“高级系统设置”—-“环境变量”—-系统变量下<br>的“新建”：</p>
<p><strong>环境变量：</strong><br><strong>JAVA_HOME：</strong>D:\Java\jdk1.8.0_25<br><strong>CLASSPATH  ：</strong>.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;<br><strong>Path：</strong>;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin</p>
<h1 id="常用快捷键"><a href="#常用快捷键" class="headerlink" title="常用快捷键"></a>常用快捷键</h1><p>Ctrl+C: 复制<br>Ctrl+V: 粘贴<br>Ctrl+X: 剪切<br>Ctrl+D: 在当前行下方复制一行<br>Ctrl+Y: 删除当前行<br>Ctrl+G: 快捷行数定位<br>Ctrl+E: 查看最近打开的文件<br>Ctrl+N: 查找类名，文件名<br>Ctrl+F: 类内搜索<br>Ctrl+R: 查找替换<br>Ctrl+Alt+L: 格式化代码</p>
<h2 id="构件"><a href="#构件" class="headerlink" title="构件"></a>构件</h2><p><strong>活动、片段、视图、意图、服务和内容提供者</strong></p>
<p><strong>活动（Activity）</strong>，是一个用户界面屏幕。应用可以定义一个或多个活动，用于处理应用程序的不同阶段。</p>
<p><strong>片段（Fragment）</strong>，是活动的一个组成部分，通常显示在屏幕上，但并非必须如此。通过片段，能够使应用轻松适应不同尺寸屏幕。</p>
<p><strong>视图（View）</strong>，是最小的用户界面单元，可以直接包含在活动中，也可以包含在活动的片段中。视图可以用Java代码创建，但更好的方式是使用XML布局来定义。</p>
<p><strong>意图（Intent）</strong>，是一种行为描述机制（如选择照片，打电话等）。在Android中，几乎一切都是通过意图来实现的，这给我们提供了大量替换或重用组件的机会。</p>
<p><strong>服务（Service）</strong>,是运行在后台的任务，无需用户直接与之交互。</p>
<p><strong>内容提供者</strong>，是一组数据和用于读取它们的自定义API，这是在应用之间共享全局数据的最好方法。</p>
<blockquote>
<p>下面重要了，上课学不到的，在这里统统学习一下，学习靠自己</p>
</blockquote>
<p><a href="https://link.jianshu.com?t=http://www.open-open.com/lib/view/open1468118887690.html" target="_blank" rel="noopener">Android Studio2.0 教程从入门到精通Windows版 - 安装篇</a></p>
<p><a href="https://link.jianshu.com?t=http://www.open-open.com/lib/view/open1468121363300.html" target="_blank" rel="noopener">Android Studio2.0 教程从入门到精通Windows版 - 入门篇</a></p>
<p><a href="https://link.jianshu.com?t=http://www.open-open.com/lib/view/open1468121618489.html" target="_blank" rel="noopener">Android Studio2.0 教程从入门到精通Windows版 - 提高篇</a></p>
<p><a href="https://link.jianshu.com?t=http://www.open-open.com/lib/view/open1466430392743.html" target="_blank" rel="noopener">Android Studio2.0 教程从入门到精通MAC版 - 安装篇</a></p>
<p><a href="https://link.jianshu.com?t=http://www.open-open.com/lib/view/open1466732896151.html" target="_blank" rel="noopener">Android Studio2.0 教程从入门到精通MAC版 - 入门篇</a></p>
<p><a href="https://link.jianshu.com?t=http://www.open-open.com/lib/view/open1466732917214.html" target="_blank" rel="noopener">Android Studio2.0 教程从入门到精通MAC版 - 提高篇</a></p>
<h2 id="授人以鱼，不如授人以渔"><a href="#授人以鱼，不如授人以渔" class="headerlink" title="授人以鱼，不如授人以渔"></a>授人以鱼，不如授人以渔</h2><p>（1）学习android需要积极的心态</p>
<p>（2）注重实践</p>
<p>（3）学以致用</p>
<p>（4）多敲代码</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了Android精通教程-第一节Android入门简介，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/13/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/13/1/" itemprop="url">AndroidStudio制作欢迎界面与应用图标</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-13T15:30:55+08:00">
                2018-03-13
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android-Studio实战练习V2/" itemprop="url" rel="index">
                    <span itemprop="name">Android Studio实战练习V2</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  714
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  3
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>AndroidStudio制作欢迎界面与应用图标</code>的概述，希望你们喜欢</p>
<h2 id="欢迎界面与应用图标"><a href="#欢迎界面与应用图标" class="headerlink" title="欢迎界面与应用图标"></a>欢迎界面与应用图标</h2><p>本项目使用Android Studio 3.0.1作为开发工具</p>
<h2 id="activity-splash-xml"><a href="#activity-splash-xml" class="headerlink" title="activity_splash.xml"></a>activity_splash.xml</h2><p>创建了一个activity_splash.xml布局文件，为欢迎界面布局，首先修改为 RelativeLayout 布局<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;!--添加一张欢迎界面的背景--&gt;</span><br><span class="line">&lt;RelativeLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:layout_width = &quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@drawable/launch_bg&quot;&gt;</span><br><span class="line">    &lt;!--显示版本号--&gt;</span><br><span class="line">    &lt;TextView</span><br><span class="line">        android:id=&quot;@+id/tv_version&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:textColor=&quot;@android:color/white&quot;</span><br><span class="line">        android:textSize=&quot;14sp&quot;</span><br><span class="line">        android:layout_centerInParent=&quot;true&quot;/&gt;</span><br><span class="line">&lt;/RelativeLayout&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="SplashActivity-java"><a href="#SplashActivity-java" class="headerlink" title="SplashActivity.java"></a>SplashActivity.java</h2><p>创建 SplashActivity.java 类，欢迎界面实现类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">package cn.edu.gdmec.android.androidstudiodemo;</span><br><span class="line"></span><br><span class="line">import android.content.Intent;</span><br><span class="line">import android.content.pm.ActivityInfo;</span><br><span class="line">import android.content.pm.PackageInfo;</span><br><span class="line">import android.content.pm.PackageManager;</span><br><span class="line">import android.os.Bundle;</span><br><span class="line">import android.support.v7.app.AppCompatActivity;</span><br><span class="line">import android.widget.TextView;</span><br><span class="line">import java.util.Timer;</span><br><span class="line">import java.util.TimerTask;</span><br><span class="line"></span><br><span class="line">public class SplashActivity extends AppCompatActivity&#123;</span><br><span class="line">    @Override</span><br><span class="line">    protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">        super.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_splash);</span><br><span class="line">        //设置此界面为</span><br><span class="line">        // 竖屏</span><br><span class="line">        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);</span><br><span class="line">        init();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void init() &#123;</span><br><span class="line">        TextView tv_version = findViewById(R.id.tv_version);</span><br><span class="line">        try &#123;</span><br><span class="line">            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),0);</span><br><span class="line">            tv_version.setText(&quot;version:&quot;+packageInfo.versionName);</span><br><span class="line">        &#125; catch (PackageManager.NameNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            tv_version.setText(&quot;version&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        //利用timer让此界面延迟3秒后跳转，timer有一个线程，该线程不断执行task</span><br><span class="line">        Timer timer = new Timer();</span><br><span class="line">        TimerTask timerTask = new TimerTask() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void run() &#123;</span><br><span class="line">                //发送intent实现页面跳转，第一个参数为当前页面的context，第二个参数为要跳转的主页</span><br><span class="line">                Intent intent = new Intent(SplashActivity.this,MainActivity.class);</span><br><span class="line">                startActivity(intent);</span><br><span class="line">                //跳转后关闭当前欢迎页面</span><br><span class="line">                SplashActivity.this.finish();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        //调度执行timerTask，第二个参数传入延迟时间（毫秒）</span><br><span class="line">        timer.schedule(timerTask,3000);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="AndroidManifest-xml"><a href="#AndroidManifest-xml" class="headerlink" title="AndroidManifest.xml"></a>AndroidManifest.xml</h2><p>在清单文件 AndroidManifest.xml 中配置欢迎界面，将应用入口修改为欢迎界面，再去除 ActionBar 效果<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    package=&quot;cn.edu.gdmec.android.androidstudiodemo&quot;&gt;</span><br><span class="line">    &lt;!--原为android:theme=&quot;@style/AppTheme&quot;--&gt;</span><br><span class="line">    &lt;!--去除ActionBar标题栏--&gt;</span><br><span class="line">    &lt;!--添加应用图标，app_icon--&gt;</span><br><span class="line">    &lt;application</span><br><span class="line">        android:allowBackup=&quot;true&quot;</span><br><span class="line">        android:icon=&quot;@drawable/app_icon&quot;</span><br><span class="line">        android:label=&quot;@string/app_name&quot;</span><br><span class="line">        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;</span><br><span class="line">        android:supportsRtl=&quot;true&quot;</span><br><span class="line">        android:theme=&quot;@style/Theme.AppCompat.NoActionBar&quot;&gt;</span><br><span class="line">        &lt;activity android:name=&quot;.SplashActivity&quot;&gt;</span><br><span class="line">            &lt;intent-filter&gt;</span><br><span class="line">                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;</span><br><span class="line">                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;</span><br><span class="line">            &lt;/intent-filter&gt;</span><br><span class="line">        &lt;/activity&gt;</span><br><span class="line">        &lt;!--添加实现类--&gt;</span><br><span class="line">        &lt;activity android:name=&quot;.MainActivity&quot;&gt;&lt;/activity&gt;</span><br><span class="line">    &lt;/application&gt;</span><br><span class="line">&lt;/manifest&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="补充"><a href="#补充" class="headerlink" title="补充"></a>补充</h2><p>该制作过程中有如背景图片的加入，可以自行找相关图片的加入或者独自DIY。其中MainActivity.java和activity_main.xml未做过任何改变。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了AndroidStudio制作欢迎界面与应用图标，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/12/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/12/1/" itemprop="url">AndroidStudio制作底部导航栏以及用Fragment实现切换功能</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-12T14:52:13+08:00">
                2018-03-12
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android-Studio实战练习V2/" itemprop="url" rel="index">
                    <span itemprop="name">Android Studio实战练习V2</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,339
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>AndroidStudio制作底部导航栏以及用Fragment实现切换功能</code>的概述，希望你们喜欢</p>
<h2 id="学习目标"><a href="#学习目标" class="headerlink" title="学习目标"></a>学习目标</h2><p>AndroidStudio制作底部导航栏以及用Fragment实现切换功能，用户点击底部导航栏可以实现三个模块的跳转。</p>
<h2 id="图片资源"><a href="#图片资源" class="headerlink" title="图片资源"></a>图片资源</h2><p>需要底部导航栏三个点击按钮的图片资源<br>main_button_1.png,main_button_2.png,main_button_3.png</p>
<p>以及点击变换的图片资源<br>main_button_1_selected.png,<br>main_button_2_selected.png,<br>main_button_3_selected.png.</p>
<p>以上图片资源都放进drawable文件夹中</p>
<h2 id="activity-main-布局"><a href="#activity-main-布局" class="headerlink" title="activity_main 布局"></a>activity_main 布局</h2><p>在 MainActivity 页面中主要有两个区域：</p>
<ul>
<li>一个是放 Fragment 的 main_body</li>
<li>一个是放底部导航栏的 main_bottom_bar</li>
</ul>
<p>主要的Fragment代码块：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;LinearLayout</span><br><span class="line">        android:orientation=&quot;vertical&quot;</span><br><span class="line">        android:background=&quot;@android:color/white&quot;</span><br><span class="line">        android:layout_width=&quot;match_parent&quot;</span><br><span class="line">        android:layout_height=&quot;match_parent&quot;&gt;</span><br><span class="line">        &lt;!--标题栏--&gt;</span><br><span class="line">        &lt;include layout=&quot;@layout/main_title_bar&quot; /&gt;</span><br><span class="line">        &lt;!--放置Fragment的main_body--&gt;</span><br><span class="line">        &lt;RelativeLayout</span><br><span class="line">            android:id=&quot;@+id/main_body&quot;</span><br><span class="line">            android:background=&quot;@android:color/white&quot;</span><br><span class="line">            android:layout_width=&quot;match_parent&quot;</span><br><span class="line">            android:layout_height=&quot;match_parent&quot;&gt;</span><br><span class="line">        &lt;/RelativeLayout&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<p>主要的底部导航栏的代码块：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--实现在底部，水平排列按钮--&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line">     android:id=&quot;@+id/main_bottom_bar&quot;</span><br><span class="line">     android:layout_alignParentBottom=&quot;true&quot;</span><br><span class="line">     android:background=&quot;#F2F2F2&quot;</span><br><span class="line">     android:orientation=&quot;horizontal&quot;</span><br><span class="line">     android:layout_width=&quot;match_parent&quot;</span><br><span class="line">     android:layout_height=&quot;55dp&quot;&gt;</span><br><span class="line">    &lt;RelativeLayout</span><br><span class="line">         android:layout_weight=&quot;1&quot;</span><br><span class="line">         android:id=&quot;@+id/bottom_bar_1_btn&quot;</span><br><span class="line">         android:layout_width=&quot;0dp&quot;</span><br><span class="line">         android:layout_height=&quot;match_parent&quot;&gt;</span><br><span class="line">         &lt;TextView</span><br><span class="line">             android:id=&quot;@+id/bottom_bar_text_1&quot;</span><br><span class="line">             android:layout_width=&quot;match_parent&quot;</span><br><span class="line">             android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">             android:layout_alignParentBottom=&quot;true&quot;</span><br><span class="line">             android:layout_centerHorizontal=&quot;true&quot;</span><br><span class="line">             android:layout_marginBottom=&quot;3dp&quot;</span><br><span class="line">             android:gravity=&quot;center&quot;</span><br><span class="line">             android:singleLine=&quot;true&quot;</span><br><span class="line">             android:text=&quot;button_1&quot;</span><br><span class="line">             android:textColor=&quot;#666666&quot;</span><br><span class="line">             android:textSize=&quot;14sp&quot;/&gt;</span><br><span class="line">         &lt;ImageView</span><br><span class="line">             android:layout_width=&quot;27dp&quot;</span><br><span class="line">             android:layout_height=&quot;27dp&quot;</span><br><span class="line">             android:layout_above=&quot;@+id/bottom_bar_text_1&quot;</span><br><span class="line">             android:layout_alignParentTop=&quot;true&quot;</span><br><span class="line">             android:layout_centerHorizontal=&quot;true&quot;</span><br><span class="line">             android:layout_marginTop=&quot;3dp&quot;</span><br><span class="line">             android:id=&quot;@+id/bottom_bar_image_1&quot;</span><br><span class="line">             android:src=&quot;@drawable/main_button_1&quot;/&gt;</span><br><span class="line">     &lt;/RelativeLayout&gt;</span><br><span class="line">    ....&lt;!--布局的代码总是繁琐又无聊的，记得要自己补全--&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="实例化控件"><a href="#实例化控件" class="headerlink" title="实例化控件"></a>实例化控件</h2><p>实例化控件一些琐碎的代码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">//先实例化控件，那我给出自己打的实例化代码</span><br><span class="line">//来自main_title_bar.xml</span><br><span class="line">    private TextView tv_main_title;//标题</span><br><span class="line">    private TextView tv_back;//返回按钮</span><br><span class="line">    private RelativeLayout title_bar;</span><br><span class="line">//来自activity_main.xml</span><br><span class="line">    private RelativeLayout main_body;</span><br><span class="line">    private TextView bottom_bar_text_1;</span><br><span class="line">    private ImageView bottom_bar_image_1;</span><br><span class="line">    ...</span><br><span class="line">    private LinearLayout main_bottom_bar;</span><br><span class="line">    private RelativeLayout bottom_bar_1_btn;</span><br><span class="line">    private RelativeLayout ...;</span><br></pre></td></tr></table></figure></p>
<p>然后<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">initView();</span><br><span class="line">//实例化</span><br><span class="line">private void initView()&#123;</span><br><span class="line">        //标题显示</span><br><span class="line">        tv_back=findViewById(R.id.tv_back);</span><br><span class="line">        tv_main_title=findViewById(R.id.tv_main_title);</span><br><span class="line">        title_bar=findViewById(R.id.title_bar);</span><br><span class="line">        //底部导航栏</span><br><span class="line">        main_body=findViewById(R.id.main_body);</span><br><span class="line">        bottom_bar_text_1=findViewById(R.id.bottom_bar_text_1);</span><br><span class="line">        bottom_bar_image_1=findViewById(R.id.bottom_bar_image_1);</span><br><span class="line">        ...</span><br><span class="line">        //包含底部 android:id=&quot;@+id/main_bottom_bar&quot;</span><br><span class="line">        main_bottom_bar=findViewById(R.id.main_bottom_bar);</span><br><span class="line">        //private RelativeLayout bottom_bar_1_btn;</span><br><span class="line">        bottom_bar_1_btn=findViewById(R.id.bottom_bar_1_btn);</span><br><span class="line">        //添加点击事件</span><br><span class="line">       bottom_bar_1_btn.setOnClickListener(this);</span><br><span class="line">       ...</span><br><span class="line">       //技巧</span><br><span class="line">       //tv_back.setVisibility(View.GONE);</span><br><span class="line">        tv_main_title.setText(&quot;课程&quot;);</span><br><span class="line">        title_bar.setBackgroundColor(Color.parseColor(&quot;#30B4FF&quot;));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="底部导航栏状态的切换方法"><a href="#底部导航栏状态的切换方法" class="headerlink" title="底部导航栏状态的切换方法"></a>底部导航栏状态的切换方法</h2><p>给MainActivity加一个setSelectStatus() 方法，方法里用参数index来判断当前选的按钮</p>
<p>示例代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">private void setSelectStatus(int index) &#123;</span><br><span class="line">        switch (index)&#123;</span><br><span class="line">            case 0:</span><br><span class="line">              //图片点击选择变换图片，颜色的改变，其他变为原来的颜色，并保持原有的图片</span><br><span class="line">              bottom_bar_image_1.setImageResource(R.drawable.main_button_1_selected);</span><br><span class="line">              bottom_bar_text_1.setTextColor(Color.parseColor(&quot;#0097F7&quot;));</span><br><span class="line">              //其他的文本颜色不变</span><br><span class="line">              bottom_bar_text_2.setTextColor(Color.parseColor(&quot;#666666&quot;));</span><br><span class="line">              bottom_bar_text_3.setTextColor(Color.parseColor(&quot;#666666&quot;));</span><br><span class="line">              //图片也不变</span><br><span class="line">              bottom_bar_image_2.setImageResource(R.drawable.main_button_2);</span><br><span class="line">              bottom_bar_image_3.setImageResource(R.drawable.main_button_3);</span><br><span class="line">             break;</span><br><span class="line">            case 1://同理如上</span><br><span class="line">             ...</span><br><span class="line">             break;</span><br><span class="line">            case 2://同理如上</span><br><span class="line">             ...</span><br><span class="line">             break;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="实现底部导航栏的响应"><a href="#实现底部导航栏的响应" class="headerlink" title="实现底部导航栏的响应"></a>实现底部导航栏的响应</h2><p>导航栏文本颜色和图片切换效果的方法写好了，接下来是点击响应的方法</p>
<p>给MainActivity加上View.OnClickListener接口</p>
<p>在生成的 onClick() 方法中加上导航栏区域的响应<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">@Override</span><br><span class="line">    public void onClick(View v) &#123;</span><br><span class="line">        switch (v.getId())&#123;</span><br><span class="line">          case R.id.bottom_bar_1_btn:</span><br><span class="line">                setSelectStatus(0);</span><br><span class="line">                break;</span><br><span class="line">          case R.id.bottom_bar_2_btn:</span><br><span class="line">                setSelectStatus(1);</span><br><span class="line">                break;</span><br><span class="line">          case R.id.bottom_bar_3_btn:</span><br><span class="line">                setSelectStatus(2);</span><br><span class="line">                break;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>别忘了在initView() 中添加监听器<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bottom_bar_1_btn.setOnClickListener(this);</span><br></pre></td></tr></table></figure></p>
<h2 id="三个-fragment-的创建"><a href="#三个-fragment-的创建" class="headerlink" title="三个 fragment 的创建"></a>三个 fragment 的创建</h2><p>就是简单的创建三个布局，展现Fragment_1/2/3 即可</p>
<p>示例代码块<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@android:color/white&quot;&gt;</span><br><span class="line">    &lt;TextView</span><br><span class="line">        android:text=&quot;Fragment_1&quot;</span><br><span class="line">        android:textColor=&quot;@android:color/black&quot;</span><br><span class="line">        android:textSize=&quot;50sp&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot; /&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<p>然后通过我之前写的插件自动生成三个Fragemnt ，就可以了不用管生成的Fragement_1/2/3.java文件了，<br>插件文章<br>《 <a href="https://www.jianshu.com/p/9d2bf0513443" target="_blank" rel="noopener">Android开发的插件Code Generator与LayoutCreator的安装与使用，提升你的开发效率</a> 》</p>
<h2 id="三个fragment的显示和切换"><a href="#三个fragment的显示和切换" class="headerlink" title="三个fragment的显示和切换"></a>三个fragment的显示和切换</h2><p>在MainActivity里把AppCompatActivity改为FragmentActivity</p>
<p>把Fragment加到Activity里的代码<br>通常用这个来展示，但是代码过长，我们来简化一下<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line">* FragmentManager manager = getSupportFragmentManager();</span><br><span class="line">* FragmentTransaction transaction = manager.beginTransaction();</span><br><span class="line">* transaction.add(R.id.main_body,new CourseFragment()).commit();</span><br><span class="line">* */</span><br></pre></td></tr></table></figure></p>
<p>我们先来添加一个setMain() 方法，来显示打开界面时，显示的初始页面<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">/用于打开初始页面</span><br><span class="line"> private void setMain() &#123;</span><br><span class="line">      //getSupportFragmentManager() -&gt; beginTransaction() -&gt; add -&gt; (R.id.main_boy,显示课程 new CourseFragment()</span><br><span class="line">      this.getSupportFragmentManager().beginTransaction().add(R.id.main_body,new CourseFragment()).commit();</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure></p>
<p>上面的代码中可以看到相对来说比较少，那我们就用这个，然后我们来实现点击底部导航栏来切换响应的fragment,我们在onClick()中添加即可。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">case R.id.bottom_bar_1_btn:</span><br><span class="line">                //添加</span><br><span class="line">                getSupportFragmentManager().beginTransaction().replace(R.id.main_body,new Button_1Fragment()).commit();</span><br><span class="line">                setSelectStatus(0);</span><br><span class="line">                break;</span><br></pre></td></tr></table></figure></p>
<p>如果觉得不错，那就点个赞吧！❤️</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了AndroidStudio制作底部导航栏以及用Fragment实现切换功能，界面的布局介绍，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/11/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/11/1/" itemprop="url">AndroidStudio制作登录和注册功能的实现，界面的布局介绍</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-11T15:29:49+08:00">
                2018-03-11
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android-Studio实战练习V2/" itemprop="url" rel="index">
                    <span itemprop="name">Android Studio实战练习V2</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  4,495
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  21
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>AndroidStudio制作登录和注册功能的实现，界面的布局介绍</code>的概述，希望你们喜欢</p>
<h1 id="每日一句："><a href="#每日一句：" class="headerlink" title="每日一句："></a>每日一句：</h1><p>Success is connecting with the world and making people feel. - 《名扬四海》<br><img src="https://upload-images.jianshu.io/upload_images/11158618-50b207ed735d857d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="每日一句"></p>
<h2 id="设计思路"><a href="#设计思路" class="headerlink" title="设计思路"></a>设计思路</h2><p>当我们面临制作登录和注册功能的实现时，我们需要先<strong>设计登录界面的布局和注册界面的布局，做到有完整的思路时</strong>才开始实现其功能效果会更好。</p>
<p>我们需要做个<strong>标题栏，登陆界面，实现登陆界面的功能代码块，注册界面，实现测试界面的功能模块</strong>即可完成。</p>
<h2 id="标题栏的设计思路"><a href="#标题栏的设计思路" class="headerlink" title="标题栏的设计思路"></a>标题栏的设计思路</h2><p>每个APP都基本上有个标题栏，即是显示标题，标题栏的两侧大多数都有一个返回建。那么标题栏即是一个返回键和一个标题栏的制作布局。</p>
<p>为了避免大多数代码的冗杂，我们把这个标题栏的制作布局独立起来，标题的显示我们可以在每块主题模块上，用<code>setText()</code>方法来显示不同的标题。</p>
<p>接下来我们创建<code>main_title_bar.xml</code>布局文件：<br>具体代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;!--这里代码的是创建一个标题栏，左边是返回键--&gt;</span><br><span class="line">&lt;!--我们设置RelativeLayout布局，id = &quot;title_bar&quot;--&gt;</span><br><span class="line">&lt;RelativeLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:id=&quot;@+id/title_bar&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;50dp&quot;</span><br><span class="line">    android:background=&quot;@android:color/transparent&quot;&gt;</span><br><span class="line">    &lt;!--一个是显示返回键，一个是显示标题框--&gt;</span><br><span class="line">    &lt;!--通过TextView来显示，id ： tv_back , tv_main_title --&gt;</span><br><span class="line">    &lt;TextView</span><br><span class="line">        android:id=&quot;@+id/tv_back&quot;</span><br><span class="line">        android:layout_width=&quot;50dp&quot;</span><br><span class="line">        android:layout_height=&quot;50dp&quot;</span><br><span class="line">        android:layout_alignParentLeft=&quot;true&quot;</span><br><span class="line">        android:background=&quot;@drawable/go_back_selector&quot; /&gt;</span><br><span class="line">    &lt;TextView</span><br><span class="line">        android:id=&quot;@+id/tv_main_title&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_centerInParent=&quot;true&quot;</span><br><span class="line">        android:textColor=&quot;@android:color/white&quot;</span><br><span class="line">        android:textSize=&quot;20sp&quot;/&gt;</span><br><span class="line">    &lt;!--其中android:background=&quot;@drawable/go_back_selector&quot;为点击回退键时，会变化效果，其实就是一个点击更换个图片而已。--&gt;</span><br><span class="line">    &lt;!--我们先用这种老方法，接下来以后的文章才做代码优化效果--&gt;</span><br><span class="line">&lt;/RelativeLayout&gt;</span><br></pre></td></tr></table></figure></p>
<h3 id="补充："><a href="#补充：" class="headerlink" title="补充："></a>补充：</h3><p>现在标题栏布局做好了，我们需要了解怎么换图片，就是在<code>android:background=&quot;@drawable/go_back_selector&quot;</code>,其实就是在drawable中创建这个<code>go_back_selector.xml</code>文件而已，用到了<code>android:state_pressed=&quot;true&quot;</code>这个属性，当点击时就是变化的图片效果，记住state_pressed就OK。</p>
<h2 id="登录界面布局"><a href="#登录界面布局" class="headerlink" title="登录界面布局"></a>登录界面布局</h2><p>创建登录界面，我们需要标题栏显示“登录”，那么就要通过<include>标签。</include></p>
<p>我们需要设计想好美化登录界面，需要以下图片：登录背景图片<code>login_bg.png</code>,默认的头像图片<code>default_icon</code>,输入用户名的背景图片<code>login_user_name_bg</code>,在用户名前需要一个小标图<code>user_name_icon</code>,同理，输入密码框需要图片有<code>login_psw_bg,psw_icon</code>,按钮需要图片加以美观<code>register_selector</code>,根据需要的图片可自行制作。</p>
<h2 id="登录界面布局模块代码"><a href="#登录界面布局模块代码" class="headerlink" title="登录界面布局模块代码"></a>登录界面布局模块代码</h2><p>创建activity_login.xml布局文件，具体代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;!--登录界面,用LinearLayout--&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@drawable/login_bg&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line">    &lt;!--标题栏--&gt;</span><br><span class="line">    &lt;include layout=&quot;@layout/main_title_bar&quot;&gt;&lt;/include&gt;</span><br><span class="line">    &lt;!--显示头像，记得加入id iv_head --&gt;</span><br><span class="line">    &lt;ImageView</span><br><span class="line">        android:id=&quot;@+id/iv_head&quot;</span><br><span class="line">        android:layout_width=&quot;70dp&quot;</span><br><span class="line">        android:layout_height=&quot;70dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;25dp&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:background=&quot;@drawable/default_icon&quot;/&gt;</span><br><span class="line">    &lt;!--输入框--&gt;</span><br><span class="line">    &lt;EditText</span><br><span class="line">        android:id=&quot;@+id/et_user_name&quot;</span><br><span class="line">        android:layout_width=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_height=&quot;48dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginLeft=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginRight=&quot;35dp&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:background=&quot;@drawable/login_user_name_bg&quot;</span><br><span class="line">        android:drawableLeft=&quot;@drawable/user_name_icon&quot;</span><br><span class="line">        android:drawablePadding=&quot;10dp&quot;</span><br><span class="line">        android:paddingLeft=&quot;8dp&quot;</span><br><span class="line">        android:gravity=&quot;center_vertical&quot;</span><br><span class="line">        android:hint=&quot;请输入用户名&quot;</span><br><span class="line">        android:singleLine=&quot;true&quot;</span><br><span class="line">        android:textColor=&quot;#000000&quot;</span><br><span class="line">        android:textColorHint=&quot;#a3a3a3&quot;</span><br><span class="line">        android:textSize=&quot;14sp&quot;/&gt;</span><br><span class="line">    &lt;!--输入框--&gt;</span><br><span class="line">    &lt;EditText</span><br><span class="line">        android:id=&quot;@+id/et_psw&quot;</span><br><span class="line">        android:layout_width=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_height=&quot;48dp&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:layout_marginLeft=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginRight=&quot;35dp&quot;</span><br><span class="line">        android:background=&quot;@drawable/login_psw_bg&quot;</span><br><span class="line">        android:drawableLeft=&quot;@drawable/psw_icon&quot;</span><br><span class="line">        android:drawablePadding=&quot;10dp&quot;</span><br><span class="line">        android:paddingLeft=&quot;8dp&quot;</span><br><span class="line">        android:gravity=&quot;center_vertical&quot;</span><br><span class="line">        android:hint=&quot;请输入密码&quot;</span><br><span class="line">        android:inputType=&quot;textPassword&quot;</span><br><span class="line">        android:singleLine=&quot;true&quot;</span><br><span class="line">        android:textColor=&quot;#000000&quot;</span><br><span class="line">        android:textColorHint=&quot;#a3a3a3&quot;</span><br><span class="line">        android:textSize=&quot;14sp&quot;/&gt;</span><br><span class="line">     &lt;!--按钮--&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/btn_login&quot;</span><br><span class="line">        android:layout_width=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_height=&quot;40dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">        android:layout_marginLeft=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginRight=&quot;35dp&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:background=&quot;@drawable/register_selector&quot;</span><br><span class="line">        android:text=&quot;登 录&quot;</span><br><span class="line">        android:textColor=&quot;@android:color/white&quot;</span><br><span class="line">        android:textSize=&quot;18sp&quot;/&gt;</span><br><span class="line">     &lt;!--显示tv register , find_psw --&gt;</span><br><span class="line">    &lt;LinearLayout</span><br><span class="line">        android:layout_width=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_height=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_marginTop=&quot;8dp&quot;</span><br><span class="line">        android:layout_marginLeft=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginRight=&quot;35dp&quot;</span><br><span class="line">        android:gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:orientation=&quot;horizontal&quot;&gt;</span><br><span class="line">        &lt;TextView</span><br><span class="line">            android:id=&quot;@+id/tv_register&quot;</span><br><span class="line">            android:layout_width=&quot;0dp&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_weight=&quot;1&quot;</span><br><span class="line">            android:gravity=&quot;center_horizontal&quot;</span><br><span class="line">            android:padding=&quot;8dp&quot;</span><br><span class="line">            android:text=&quot;立即注册&quot;</span><br><span class="line">            android:textColor=&quot;@android:color/white&quot;</span><br><span class="line">            android:textSize=&quot;14sp&quot; /&gt;</span><br><span class="line">        &lt;!--layout_weight=&quot;1&quot; layout_width=&quot;0dp&quot;实现均分效果--&gt;</span><br><span class="line">        &lt;TextView</span><br><span class="line">            android:id=&quot;@+id/tv_find_psw&quot;</span><br><span class="line">            android:layout_width=&quot;0dp&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_weight=&quot;1&quot;</span><br><span class="line">            android:gravity=&quot;center_horizontal&quot;</span><br><span class="line">            android:padding=&quot;8dp&quot;</span><br><span class="line">            android:text=&quot;找回密码?&quot;</span><br><span class="line">            android:textColor=&quot;@android:color/white&quot;</span><br><span class="line">            android:textSize=&quot;14sp&quot; /&gt;</span><br><span class="line">    &lt;/LinearLayout&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="同理注册界面布局设计思路"><a href="#同理注册界面布局设计思路" class="headerlink" title="同理注册界面布局设计思路"></a>同理注册界面布局设计思路</h2><p>创建注册界面，我们需要标题栏显示“注册”，那么就要通过<include>标签。那么我做了效果图，提供思路参考：<br><img src="https://upload-images.jianshu.io/upload_images/11158618-f91e587f11fbd3f1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="注册界面思路图"></include></p>
<h2 id="注册布局模块代码"><a href="#注册布局模块代码" class="headerlink" title="注册布局模块代码"></a>注册布局模块代码</h2><p>创建activity_register.xml布局文件，具体代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;!--注册界面--&gt;</span><br><span class="line">&lt;!--这里的布局放置是： 1 个 ImageView 控件，用于显示用户头像；3 个 EditText 控件，用于输入用户名、密码、再次输入密码；1 个 Button 控件为注册按钮--&gt;</span><br><span class="line">&lt;!--修改 activity_register.xml 为 LinearLayout 布局--&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:id=&quot;@+id/activity_register&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:background=&quot;@drawable/register_bg&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line">    &lt;include layout=&quot;@layout/main_title_bar&quot;&gt;&lt;/include&gt;&lt;!--引入标题栏--&gt;</span><br><span class="line">    &lt;ImageView</span><br><span class="line">        android:layout_width=&quot;70dp&quot;</span><br><span class="line">        android:layout_height=&quot;70dp&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:layout_marginTop=&quot;25dp&quot;</span><br><span class="line">        android:src=&quot;@drawable/default_icon&quot;/&gt;</span><br><span class="line">    &lt;!--三个编辑框--&gt;</span><br><span class="line">    &lt;EditText</span><br><span class="line">        android:id=&quot;@+id/et_user_name&quot;</span><br><span class="line">        android:layout_width=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_height=&quot;48dp&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:layout_marginLeft=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginRight=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;35dp&quot;</span><br><span class="line">        android:background=&quot;@drawable/register_user_name_bg&quot;</span><br><span class="line">        android:drawableLeft=&quot;@drawable/user_name_icon&quot;</span><br><span class="line">        android:drawablePadding=&quot;10dp&quot;</span><br><span class="line">        android:gravity=&quot;center_vertical&quot;</span><br><span class="line">        android:hint=&quot;请输入用户名&quot;</span><br><span class="line">        android:paddingLeft=&quot;8dp&quot;</span><br><span class="line">        android:singleLine=&quot;true&quot;</span><br><span class="line">        android:textColor=&quot;#000000&quot;</span><br><span class="line">        android:textColorHint=&quot;#a3a3a3&quot;</span><br><span class="line">        android:textSize=&quot;14sp&quot;/&gt;</span><br><span class="line">    &lt;EditText</span><br><span class="line">        android:id=&quot;@+id/et_psw&quot;</span><br><span class="line">        android:layout_width=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:layout_height=&quot;48dp&quot;</span><br><span class="line">        android:layout_marginLeft=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginRight=&quot;35dp&quot;</span><br><span class="line">        android:background=&quot;@drawable/register_psw_bg&quot;</span><br><span class="line">        android:drawableLeft=&quot;@drawable/psw_icon&quot;</span><br><span class="line">        android:drawablePadding=&quot;10dp&quot;</span><br><span class="line">        android:hint=&quot;请输入密码&quot;</span><br><span class="line">        android:inputType=&quot;textPassword&quot;</span><br><span class="line">        android:paddingLeft=&quot;8dp&quot;</span><br><span class="line">        android:singleLine=&quot;true&quot;</span><br><span class="line">        android:textColor=&quot;#000000&quot;</span><br><span class="line">        android:textColorHint=&quot;#a3a3a3&quot;</span><br><span class="line">        android:textSize=&quot;14sp&quot;/&gt;</span><br><span class="line">    &lt;EditText</span><br><span class="line">        android:id=&quot;@+id/et_psw_again&quot;</span><br><span class="line">        android:layout_width=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_height=&quot;48dp&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:layout_marginLeft=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginRight=&quot;35dp&quot;</span><br><span class="line">        android:background=&quot;@drawable/register_psw_again_bg&quot;</span><br><span class="line">        android:drawableLeft=&quot;@drawable/psw_icon&quot;</span><br><span class="line">        android:drawablePadding=&quot;10dp&quot;</span><br><span class="line">        android:hint=&quot;请再次输入密码&quot;</span><br><span class="line">        android:inputType=&quot;textPassword&quot;</span><br><span class="line">        android:paddingLeft=&quot;8dp&quot;</span><br><span class="line">        android:singleLine=&quot;true&quot;</span><br><span class="line">        android:textColor=&quot;#000000&quot;</span><br><span class="line">        android:textColorHint=&quot;#a3a3a3&quot;</span><br><span class="line">        android:textSize=&quot;14sp&quot;/&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/btn_register&quot;</span><br><span class="line">        android:layout_width=&quot;fill_parent&quot;</span><br><span class="line">        android:layout_height=&quot;40dp&quot;</span><br><span class="line">        android:layout_gravity=&quot;center_horizontal&quot;</span><br><span class="line">        android:layout_marginLeft=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginRight=&quot;35dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;15dp&quot;</span><br><span class="line">        android:background=&quot;@drawable/register_selector&quot;</span><br><span class="line">        android:text=&quot;注 册&quot;</span><br><span class="line">        android:textColor=&quot;@android:color/white&quot;</span><br><span class="line">        android:textSize=&quot;18sp&quot;/&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="MD5算法"><a href="#MD5算法" class="headerlink" title="MD5算法"></a>MD5算法</h2><p>MD5 为 Message-Digest Algorithm 5（信息–摘要算法），记住几个要点就可以了。</p>
<ul>
<li>Message</li>
<li>Digest</li>
<li>MessageDigest</li>
<li>MessageDigest.getInstance( );<br>由于注册登录涉及密码，我们需要对用户的密码进行 MD5 算法加密，MD5 算法是把任意长度的字符串变成固定长度（通常是128位）的16进制字符串，且此算法不可逆。</li>
</ul>
<p>具体代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">package cn.edu.gdmec.android.androidstudiodemo.utils;</span><br><span class="line">import java.security.MessageDigest;</span><br><span class="line">import java.security.NoSuchAlgorithmException;</span><br><span class="line">public class MD5Utils &#123;</span><br><span class="line">    //md5 加密算法</span><br><span class="line">    public static String md5(String text) &#123;</span><br><span class="line">        MessageDigest digest = null;</span><br><span class="line">        try &#123;</span><br><span class="line">            digest = MessageDigest.getInstance(&quot;md5&quot;);</span><br><span class="line">            // 数组 byte[] result -&gt; digest.digest( );  文本 text.getBytes(); </span><br><span class="line">            byte[] result = digest.digest(text.getBytes());</span><br><span class="line">            //创建StringBuilder对象 然后建议StringBuffer，安全性高</span><br><span class="line">            //StringBuilder sb = new StringBuilder();</span><br><span class="line">            StringBuffer sb = new StringBuffer();</span><br><span class="line">            // result数组，digest.digest ( ); -&gt; text.getBytes();</span><br><span class="line">            // for 循环数组byte[] result;</span><br><span class="line">            for (byte b : result)&#123;</span><br><span class="line">                // 0xff 为16进制</span><br><span class="line">                int number = b &amp; 0xff;</span><br><span class="line">                // number值 转换 字符串 Integer.toHexString( );</span><br><span class="line">                String hex = Integer.toHexString(number);</span><br><span class="line">                if (hex.length() == 1)&#123;</span><br><span class="line">                    sb.append(&quot;0&quot;+hex);</span><br><span class="line">                &#125;else &#123;</span><br><span class="line">                    sb.append(hex);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            //sb StringBuffer sb = new StringBuffer();对象实例化</span><br><span class="line">            return sb.toString();</span><br><span class="line">        &#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            //发送异常return空字符串</span><br><span class="line">            return &quot;&quot;;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="实现注册逻辑功能代码"><a href="#实现注册逻辑功能代码" class="headerlink" title="实现注册逻辑功能代码"></a>实现注册逻辑功能代码</h2><p>完成注册页面的布局与 MD5 工具类后，进行注册界面的逻辑编写。</p>
<p>当在注册界面点击注册按钮后，需要<strong>获取用户名，用户密码和再次确认密码，当两次密码相同时，将用户名和密码（经过 MD5 加密）保存到 SharedPreferences 中</strong>，同时当注册成功后，需要将用户名传递到登录界面中。</p>
<p>RegisterActivity.java具体代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br></pre></td><td class="code"><pre><span class="line">package cn.edu.gdmec.android.androidstudiodemo;</span><br><span class="line">import android.content.Intent;</span><br><span class="line">import android.content.SharedPreferences;</span><br><span class="line">import android.content.pm.ActivityInfo;</span><br><span class="line">import android.graphics.Color;</span><br><span class="line">import android.os.Bundle;</span><br><span class="line">import android.support.v7.app.AppCompatActivity;</span><br><span class="line">import android.text.TextUtils;</span><br><span class="line">import android.view.View;</span><br><span class="line">import android.widget.Button;</span><br><span class="line">import android.widget.EditText;</span><br><span class="line">import android.widget.RelativeLayout;</span><br><span class="line">import android.widget.TextView;</span><br><span class="line">import android.widget.Toast;</span><br><span class="line">import cn.edu.gdmec.android.androidstudiodemo.utils.MD5Utils;</span><br><span class="line"></span><br><span class="line">public class RegisterActivity extends AppCompatActivity &#123;</span><br><span class="line">    private TextView tv_main_title;//标题</span><br><span class="line">    private TextView tv_back;//返回按钮</span><br><span class="line">    private Button btn_register;//注册按钮</span><br><span class="line">    //用户名，密码，再次输入的密码的控件</span><br><span class="line">    private EditText et_user_name,et_psw,et_psw_again;</span><br><span class="line">    //用户名，密码，再次输入的密码的控件的获取值</span><br><span class="line">    private String userName,psw,pswAgain;</span><br><span class="line">    //标题布局</span><br><span class="line">    private RelativeLayout rl_title_bar;</span><br><span class="line">    @Override</span><br><span class="line">    protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">        super.onCreate(savedInstanceState);</span><br><span class="line">        //设置页面布局 ,注册界面</span><br><span class="line">        setContentView(R.layout.activity_register);</span><br><span class="line">        //设置此界面为竖屏</span><br><span class="line">        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);</span><br><span class="line">        init();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void init() &#123;</span><br><span class="line">        //从main_title_bar.xml 页面布局中获取对应的UI控件</span><br><span class="line">        tv_main_title=findViewById(R.id.tv_main_title);</span><br><span class="line">        tv_main_title.setText(&quot;注册&quot;);</span><br><span class="line">        tv_back=findViewById(R.id.tv_back);</span><br><span class="line">        //布局根元素</span><br><span class="line">        rl_title_bar=findViewById(R.id.title_bar);</span><br><span class="line">        rl_title_bar.setBackgroundColor(Color.TRANSPARENT);</span><br><span class="line">        //从activity_register.xml 页面中获取对应的UI控件</span><br><span class="line">        btn_register=findViewById(R.id.btn_register);</span><br><span class="line">        et_user_name=findViewById(R.id.et_user_name);</span><br><span class="line">        et_psw=findViewById(R.id.et_psw);</span><br><span class="line">        et_psw_again=findViewById(R.id.et_psw_again);</span><br><span class="line">        tv_back.setOnClickListener(new View.OnClickListener() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void onClick(View v) &#123;</span><br><span class="line">                //返回键</span><br><span class="line">                RegisterActivity.this.finish();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        //注册按钮</span><br><span class="line">        btn_register.setOnClickListener(new View.OnClickListener() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void onClick(View v) &#123;</span><br><span class="line">                //获取输入在相应控件中的字符串</span><br><span class="line">                getEditString();</span><br><span class="line">                //判断输入框内容</span><br><span class="line">                if(TextUtils.isEmpty(userName))&#123;</span><br><span class="line">                    Toast.makeText(RegisterActivity.this, &quot;请输入用户名&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    return;</span><br><span class="line">                &#125;else if(TextUtils.isEmpty(psw))&#123;</span><br><span class="line">                    Toast.makeText(RegisterActivity.this, &quot;请输入密码&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    return;</span><br><span class="line">                &#125;else if(TextUtils.isEmpty(pswAgain))&#123;</span><br><span class="line">                    Toast.makeText(RegisterActivity.this, &quot;请再次输入密码&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    return;</span><br><span class="line">                &#125;else if(!psw.equals(pswAgain))&#123;</span><br><span class="line">                    Toast.makeText(RegisterActivity.this, &quot;输入两次的密码不一样&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    return;</span><br><span class="line">                    /**</span><br><span class="line">                     *从SharedPreferences中读取输入的用户名，判断SharedPreferences中是否有此用户名</span><br><span class="line">                     */</span><br><span class="line">                &#125;else if(isExistUserName(userName))&#123;</span><br><span class="line">                    Toast.makeText(RegisterActivity.this, &quot;此账户名已经存在&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    return;</span><br><span class="line">                &#125;else&#123;</span><br><span class="line">                    Toast.makeText(RegisterActivity.this, &quot;注册成功&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    //把账号、密码和账号标识保存到sp里面</span><br><span class="line">                    /**</span><br><span class="line">                     * 保存账号和密码到SharedPreferences中</span><br><span class="line">                     */</span><br><span class="line">                    saveRegisterInfo(userName, psw);</span><br><span class="line">                    //注册成功后把账号传递到LoginActivity.java中</span><br><span class="line">                    // 返回值到loginActivity显示</span><br><span class="line">                    Intent data = new Intent();</span><br><span class="line">                    data.putExtra(&quot;userName&quot;, userName);</span><br><span class="line">                    setResult(RESULT_OK, data);</span><br><span class="line">                    //RESULT_OK为Activity系统常量，状态码为-1，</span><br><span class="line">                    // 表示此页面下的内容操作成功将data返回到上一页面，如果是用back返回过去的则不存在用setResult传递data值</span><br><span class="line">                    RegisterActivity.this.finish();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     * 获取控件中的字符串</span><br><span class="line">     */</span><br><span class="line">    private void getEditString()&#123;</span><br><span class="line">        userName=et_user_name.getText().toString().trim();</span><br><span class="line">        psw=et_psw.getText().toString().trim();</span><br><span class="line">        pswAgain=et_psw_again.getText().toString().trim();</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     * 从SharedPreferences中读取输入的用户名，判断SharedPreferences中是否有此用户名</span><br><span class="line">     */</span><br><span class="line">    private boolean isExistUserName(String userName)&#123;</span><br><span class="line">        boolean has_userName=false;</span><br><span class="line">        //mode_private SharedPreferences sp = getSharedPreferences( );</span><br><span class="line">        // &quot;loginInfo&quot;, MODE_PRIVATE</span><br><span class="line">        SharedPreferences sp=getSharedPreferences(&quot;loginInfo&quot;, MODE_PRIVATE);</span><br><span class="line">        //获取密码</span><br><span class="line">        String spPsw=sp.getString(userName, &quot;&quot;);//传入用户名获取密码</span><br><span class="line">        //如果密码不为空则确实保存过这个用户名</span><br><span class="line">        if(!TextUtils.isEmpty(spPsw)) &#123;</span><br><span class="line">            has_userName=true;</span><br><span class="line">        &#125;</span><br><span class="line">        return has_userName;</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     * 保存账号和密码到SharedPreferences中SharedPreferences</span><br><span class="line">     */</span><br><span class="line">    private void saveRegisterInfo(String userName,String psw)&#123;</span><br><span class="line">        String md5Psw = MD5Utils.md5(psw);//把密码用MD5加密</span><br><span class="line">        //loginInfo表示文件名, mode_private SharedPreferences sp = getSharedPreferences( );</span><br><span class="line">        SharedPreferences sp=getSharedPreferences(&quot;loginInfo&quot;, MODE_PRIVATE);</span><br><span class="line">        //获取编辑器， SharedPreferences.Editor  editor -&gt; sp.edit();</span><br><span class="line">        SharedPreferences.Editor editor=sp.edit();</span><br><span class="line">        //以用户名为key，密码为value保存在SharedPreferences中</span><br><span class="line">        //key,value,如键值对，editor.putString(用户名，密码）;</span><br><span class="line">        editor.putString(userName, md5Psw);</span><br><span class="line">        //提交修改 editor.commit();</span><br><span class="line">        editor.commit();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="实现登录逻辑功能代码"><a href="#实现登录逻辑功能代码" class="headerlink" title="实现登录逻辑功能代码"></a>实现登录逻辑功能代码</h2><p>完成登录界面布局后，来实现登录界面的逻辑代码。</p>
<p>当点击登录按钮时，需判断用户名和密码是否为空。</p>
<p>若为空，则提示请输入用户名或密码，这里的判断事项比较一开始凌乱，需要细细品味；若不为空，则获取用户输入的用户名，由于用的是本地数据，需要根据用户名在 SharedPreferences 中查询是否有对应的密码，若有对应的密码且与用户输入的密码（需通过 MD5 加密）比对一致情况，则登录成功。</p>
<p>LoginActivity.java具体代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br></pre></td><td class="code"><pre><span class="line">package cn.edu.gdmec.android.androidstudiodemo;</span><br><span class="line"></span><br><span class="line">import android.content.Intent;</span><br><span class="line">import android.content.SharedPreferences;</span><br><span class="line">import android.content.pm.ActivityInfo;</span><br><span class="line">import android.os.Bundle;</span><br><span class="line">import android.support.v7.app.AppCompatActivity;</span><br><span class="line">import android.text.TextUtils;</span><br><span class="line">import android.view.View;</span><br><span class="line">import android.widget.Button;</span><br><span class="line">import android.widget.EditText;</span><br><span class="line">import android.widget.TextView;</span><br><span class="line">import android.widget.Toast;</span><br><span class="line"></span><br><span class="line">import cn.edu.gdmec.android.androidstudiodemo.utils.MD5Utils;</span><br><span class="line"></span><br><span class="line">public class LoginActivity extends AppCompatActivity&#123;</span><br><span class="line">    private TextView tv_main_title;//标题</span><br><span class="line">    private TextView tv_back,tv_register,tv_find_psw;//返回键,显示的注册，找回密码</span><br><span class="line">    private Button btn_login;//登录按钮</span><br><span class="line">    private String userName,psw,spPsw;//获取的用户名，密码，加密密码</span><br><span class="line">    private EditText et_user_name,et_psw;//编辑框</span><br><span class="line">    @Override</span><br><span class="line">    protected void onCreate(Bundle savedInstanceState)&#123;</span><br><span class="line">        super.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_login);</span><br><span class="line">        //设置此界面为竖屏</span><br><span class="line">        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);</span><br><span class="line">        init();</span><br><span class="line">    &#125;</span><br><span class="line">    //获取界面控件</span><br><span class="line">    private void init() &#123;</span><br><span class="line">        //从main_title_bar中获取的id</span><br><span class="line">        tv_main_title=findViewById(R.id.tv_main_title);</span><br><span class="line">        tv_main_title.setText(&quot;登录&quot;);</span><br><span class="line">        tv_back=findViewById(R.id.tv_back);</span><br><span class="line">        //从activity_login.xml中获取的</span><br><span class="line">        tv_register=findViewById(R.id.tv_register);</span><br><span class="line">        tv_find_psw=findViewById(R.id.tv_find_psw);</span><br><span class="line">        btn_login=findViewById(R.id.btn_login);</span><br><span class="line">        et_user_name=findViewById(R.id.et_user_name);</span><br><span class="line">        et_psw=findViewById(R.id.et_psw);</span><br><span class="line">        //返回键的点击事件</span><br><span class="line">        tv_back.setOnClickListener(new View.OnClickListener() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void onClick(View v) &#123;</span><br><span class="line">                //登录界面销毁</span><br><span class="line">                LoginActivity.this.finish();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        //立即注册控件的点击事件</span><br><span class="line">        tv_register.setOnClickListener(new View.OnClickListener() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void onClick(View v) &#123;</span><br><span class="line">                //为了跳转到注册界面，并实现注册功能</span><br><span class="line">                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);</span><br><span class="line">                startActivityForResult(intent, 1);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        //找回密码控件的点击事件</span><br><span class="line">        tv_find_psw.setOnClickListener(new View.OnClickListener() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void onClick(View v) &#123;</span><br><span class="line">                //跳转到找回密码界面（此页面暂未创建）</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        //登录按钮的点击事件</span><br><span class="line">        btn_login.setOnClickListener(new View.OnClickListener() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void onClick(View v) &#123;</span><br><span class="line">                //开始登录，获取用户名和密码 getText().toString().trim();</span><br><span class="line">                userName=et_user_name.getText().toString().trim();</span><br><span class="line">                psw=et_psw.getText().toString().trim();</span><br><span class="line">                //对当前用户输入的密码进行MD5加密再进行比对判断, MD5Utils.md5( ); psw 进行加密判断是否一致</span><br><span class="line">                String md5Psw= MD5Utils.md5(psw);</span><br><span class="line">                // md5Psw ; spPsw 为 根据从SharedPreferences中用户名读取密码</span><br><span class="line">                // 定义方法 readPsw为了读取用户名，得到密码</span><br><span class="line">                spPsw=readPsw(userName);</span><br><span class="line">                // TextUtils.isEmpty</span><br><span class="line">                if(TextUtils.isEmpty(userName))&#123;</span><br><span class="line">                    Toast.makeText(LoginActivity.this, &quot;请输入用户名&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    return;</span><br><span class="line">                &#125;else if(TextUtils.isEmpty(psw))&#123;</span><br><span class="line">                    Toast.makeText(LoginActivity.this, &quot;请输入密码&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    return;</span><br><span class="line">                    // md5Psw.equals(); 判断，输入的密码加密后，是否与保存在SharedPreferences中一致</span><br><span class="line">                &#125;else if(md5Psw.equals(spPsw))&#123;</span><br><span class="line">                    //一致登录成功</span><br><span class="line">                    Toast.makeText(LoginActivity.this, &quot;登录成功&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    //保存登录状态，在界面保存登录的用户名 定义个方法 saveLoginStatus boolean 状态 , userName 用户名;</span><br><span class="line">                    saveLoginStatus(true, userName);</span><br><span class="line">                    //登录成功后关闭此页面进入主页</span><br><span class="line">                    Intent data=new Intent();</span><br><span class="line">                    //datad.putExtra( ); name , value ;</span><br><span class="line">                    data.putExtra(&quot;isLogin&quot;,true);</span><br><span class="line">                    //RESULT_OK为Activity系统常量，状态码为-1</span><br><span class="line">                    // 表示此页面下的内容操作成功将data返回到上一页面，如果是用back返回过去的则不存在用setResult传递data值</span><br><span class="line">                    setResult(RESULT_OK,data);</span><br><span class="line">                    //销毁登录界面</span><br><span class="line">                    LoginActivity.this.finish();</span><br><span class="line">                    //跳转到主界面，登录成功的状态传递到 MainActivity 中</span><br><span class="line">                    startActivity(new Intent(LoginActivity.this, MainActivity.class));</span><br><span class="line">                    return;</span><br><span class="line">                &#125;else if((spPsw!=null&amp;&amp;!TextUtils.isEmpty(spPsw)&amp;&amp;!md5Psw.equals(spPsw)))&#123;</span><br><span class="line">                    Toast.makeText(LoginActivity.this, &quot;输入的用户名和密码不一致&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    return;</span><br><span class="line">                &#125;else&#123;</span><br><span class="line">                    Toast.makeText(LoginActivity.this, &quot;此用户名不存在&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     *从SharedPreferences中根据用户名读取密码</span><br><span class="line">     */</span><br><span class="line">    private String readPsw(String userName)&#123;</span><br><span class="line">        //getSharedPreferences(&quot;loginInfo&quot;,MODE_PRIVATE);</span><br><span class="line">        //&quot;loginInfo&quot;,mode_private; MODE_PRIVATE表示可以继续写入</span><br><span class="line">        SharedPreferences sp=getSharedPreferences(&quot;loginInfo&quot;, MODE_PRIVATE);</span><br><span class="line">        //sp.getString() userName, &quot;&quot;;</span><br><span class="line">        return sp.getString(userName , &quot;&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     *保存登录状态和登录用户名到SharedPreferences中</span><br><span class="line">     */</span><br><span class="line">    private void saveLoginStatus(boolean status,String userName)&#123;</span><br><span class="line">        //saveLoginStatus(true, userName);</span><br><span class="line">        //loginInfo表示文件名  SharedPreferences sp=getSharedPreferences(&quot;loginInfo&quot;, MODE_PRIVATE);</span><br><span class="line">        SharedPreferences sp=getSharedPreferences(&quot;loginInfo&quot;, MODE_PRIVATE);</span><br><span class="line">        //获取编辑器</span><br><span class="line">        SharedPreferences.Editor editor=sp.edit();</span><br><span class="line">        //存入boolean类型的登录状态</span><br><span class="line">        editor.putBoolean(&quot;isLogin&quot;, status);</span><br><span class="line">        //存入登录状态时的用户名</span><br><span class="line">        editor.putString(&quot;loginUserName&quot;, userName);</span><br><span class="line">        //提交修改</span><br><span class="line">        editor.commit();</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     * 注册成功的数据返回至此</span><br><span class="line">     * @param requestCode 请求码</span><br><span class="line">     * @param resultCode 结果码</span><br><span class="line">     * @param data 数据</span><br><span class="line">     */</span><br><span class="line">    @Override</span><br><span class="line">    //显示数据， onActivityResult</span><br><span class="line">    //startActivityForResult(intent, 1); 从注册界面中获取数据</span><br><span class="line">    //int requestCode , int resultCode , Intent data</span><br><span class="line">    // LoginActivity -&gt; startActivityForResult -&gt; onActivityResult();</span><br><span class="line">    protected void onActivityResult(int requestCode, int resultCode, Intent data) &#123;</span><br><span class="line">        //super.onActivityResult(requestCode, resultCode, data);</span><br><span class="line">        super.onActivityResult(requestCode, resultCode, data);</span><br><span class="line">        if(data!=null)&#123;</span><br><span class="line">            //是获取注册界面回传过来的用户名</span><br><span class="line">            // getExtra().getString(&quot;***&quot;);</span><br><span class="line">            String userName=data.getStringExtra(&quot;userName&quot;);</span><br><span class="line">            if(!TextUtils.isEmpty(userName))&#123;</span><br><span class="line">                //设置用户名到 et_user_name 控件</span><br><span class="line">                et_user_name.setText(userName);</span><br><span class="line">                //et_user_name控件的setSelection()方法来设置光标位置</span><br><span class="line">                et_user_name.setSelection(userName.length());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="补充"><a href="#补充" class="headerlink" title="补充"></a>补充</h2><p>如做了效果，需要在清单文件中实现该类，文件的跳转，可以自己了解一下。主要介绍注册模块，登录模块。里面的注解我写的如果有不全的或者错误点，可以联系讨论。</p>
<p>接下来你看到如上代码有点多，那么我们可以进行代码的优化来减少代码量。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了AndroidStudio制作登录和注册功能的实现，界面的布局介绍，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2018/03/10/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/03/10/1/" itemprop="url">AndroidStudio项目制作倒计时模块</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-10T15:07:53+08:00">
                2018-03-10
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android项目开发实战/" itemprop="url" rel="index">
                    <span itemprop="name">Android项目开发实战</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  856
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
        
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>大家好，我是 <code>Vic</code>，今天给大家带来<code>AndroidStudio项目制作倒计时模块</code>的概述，希望你们喜欢</p>
<h2 id="项目难度"><a href="#项目难度" class="headerlink" title="项目难度"></a>项目难度</h2><p>AndroidStudio项目制作倒计时模块的难度，不是很大，就是主要用了<code>Timer</code>和<code>TimerTask</code>这两个，接着就是现实界面的一些基础效果。</p>
<h2 id="设计界面"><a href="#设计界面" class="headerlink" title="设计界面"></a>设计界面</h2><p>做个倒计时的界面就比较好想了，就如下界面控件</p>
<ul>
<li>填写倒计时时间</li>
<li>获取倒计时时间</li>
<li>显示倒计时</li>
<li>开始计时</li>
<li>停止计时<br>就在自动创建的activity_main.xml中写入代码：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;</span><br><span class="line">    tools:context=&quot;cn.edu.gdmec.android.counttime.MainActivity&quot;&gt;</span><br><span class="line">    &lt;!--填写倒计时时间--&gt;</span><br><span class="line">    &lt;EditText</span><br><span class="line">        android:id=&quot;@+id/input&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:ems=&quot;10&quot;/&gt;</span><br><span class="line">    &lt;!--获取倒计时时间--&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/get&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:text=&quot;获取倒计时时间&quot;/&gt;</span><br><span class="line">    &lt;!--显示倒计时--&gt;</span><br><span class="line">    &lt;TextView</span><br><span class="line">        android:id=&quot;@+id/time&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">    &lt;!--开始计时--&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/starttime&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:text=&quot;开始计时&quot;/&gt;</span><br><span class="line">    &lt;!--停止计时--&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/stoptime&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:text=&quot;停止计时&quot;/&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="实现功能需求"><a href="#实现功能需求" class="headerlink" title="实现功能需求"></a>实现功能需求</h2><p>接下来我们需要在MainActivity.java中现实功能模块需求，主要来显示界面和获取按钮功能效果，代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line">package cn.edu.gdmec.android.counttime;</span><br><span class="line"></span><br><span class="line">import android.os.Handler;</span><br><span class="line">import android.os.Message;</span><br><span class="line">import android.support.v7.app.AppCompatActivity;</span><br><span class="line">import android.os.Bundle;</span><br><span class="line">import android.view.View;</span><br><span class="line">import android.widget.Button;</span><br><span class="line">import android.widget.EditText;</span><br><span class="line">import android.widget.TextView;</span><br><span class="line">import java.util.Timer;</span><br><span class="line">import java.util.TimerTask;</span><br><span class="line"></span><br><span class="line">public class MainActivity extends AppCompatActivity implements View.OnClickListener &#123;</span><br><span class="line">    private EditText inputet;</span><br><span class="line">    private Button get, startTime, stopTime;</span><br><span class="line">    private TextView time;</span><br><span class="line">    private int i = 0;</span><br><span class="line">    private Timer timer = null;</span><br><span class="line">    private TimerTask task = null;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">        super.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line">        initView();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void initView() &#123;</span><br><span class="line">        inputet = findViewById(R.id.input);</span><br><span class="line">        get = findViewById(R.id.get);</span><br><span class="line">        startTime = findViewById(R.id.starttime);</span><br><span class="line">        stopTime = findViewById(R.id.stoptime);</span><br><span class="line">        time = findViewById(R.id.time);</span><br><span class="line">        get.setOnClickListener(this);</span><br><span class="line">        startTime.setOnClickListener(this);</span><br><span class="line">        stopTime.setOnClickListener(this);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void onClick(View v) &#123;</span><br><span class="line">        switch (v.getId()) &#123;</span><br><span class="line">            case R.id.get:</span><br><span class="line">                time.setText(inputet.getText().toString());</span><br><span class="line">                i = Integer.parseInt(inputet.getText().toString());</span><br><span class="line">                break;</span><br><span class="line">            case R.id.starttime:</span><br><span class="line">                startTime();</span><br><span class="line">                break;</span><br><span class="line">            case R.id.stoptime:</span><br><span class="line">                stopTime();</span><br><span class="line">                break;</span><br><span class="line">            default:</span><br><span class="line">                break;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private Handler mHandler = new Handler() &#123;</span><br><span class="line">        public void handleMessage(Message msg) &#123;</span><br><span class="line">            time.setText(msg.arg1 + &quot;&quot;);</span><br><span class="line">            startTime();</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    public void startTime() &#123;</span><br><span class="line">        timer = new Timer();</span><br><span class="line">        task = new TimerTask() &#123;</span><br><span class="line"></span><br><span class="line">            @Override</span><br><span class="line">            public void run() &#123;</span><br><span class="line">                if (i &gt; 0) &#123;   //加入判断不能小于0</span><br><span class="line">                    i--;</span><br><span class="line">                    Message message = mHandler.obtainMessage();</span><br><span class="line">                    message.arg1 = i;</span><br><span class="line">                    mHandler.sendMessage(message);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        timer.schedule(task, 1000);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void stopTime()&#123;</span><br><span class="line">        timer.cancel();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="心得重点"><a href="#心得重点" class="headerlink" title="心得重点"></a>心得重点</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">//获取的按钮实现：</span><br><span class="line">time.setText(inputet.getText().toString());</span><br><span class="line">i = Integer.parseInt(inputet.getText().toString());</span><br><span class="line">//Handler的加入</span><br><span class="line">private Handler mHandler = new Handler() &#123;</span><br><span class="line">        public void handleMessage(Message msg) &#123;</span><br><span class="line">            time.setText(msg.arg1 + &quot;&quot;);</span><br><span class="line">            startTime();</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line">//倒计时主要核心</span><br><span class="line">public void startTime() &#123;</span><br><span class="line">        timer = new Timer();</span><br><span class="line">        task = new TimerTask() &#123;</span><br><span class="line"></span><br><span class="line">            @Override</span><br><span class="line">            public void run() &#123;</span><br><span class="line">                if (i &gt; 0) &#123;   //加入判断不能小于0</span><br><span class="line">                    i--;</span><br><span class="line">                    Message message = mHandler.obtainMessage();</span><br><span class="line">                    message.arg1 = i;</span><br><span class="line">                    mHandler.sendMessage(message);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        timer.schedule(task, 1000);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>本文讲了AndroidStudio项目制作倒计时模块，如果您还有更好地理解，欢迎沟通</li>
<li>定位：分享 <code>Android</code>&amp;<code>Java</code>知识点，有兴趣可以继续关注</li>
</ul>
          
        
      
    </div>
    
    
    

      <div>
        
      </div>

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/7/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/7/">7</a><span class="page-number current">8</span><a class="page-number" href="/page/9/">9</a><a class="page-number" href="/page/10/">10</a><a class="extend next" rel="next" href="/page/9/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.gif" alt="达叔小生">
            
              <p class="site-author-name" itemprop="name">达叔小生</p>
              <p class="site-description motion-element" itemprop="description"></p>

            <div id="days"></div>
<script>
function show_date_time(){
window.setTimeout("show_date_time()", 1000);
BirthDay=new Date("04/23/1997 00:00:00");
today=new Date();
timeold=(today.getTime()-BirthDay.getTime());
sectimeold=timeold/1000
secondsold=Math.floor(sectimeold);
msPerDay=24*60*60*1000
e_daysold=timeold/msPerDay
daysold=Math.floor(e_daysold);
e_hrsold=(e_daysold-daysold)*24;
hrsold=setzero(Math.floor(e_hrsold));
e_minsold=(e_hrsold-hrsold)*60;
minsold=setzero(Math.floor((e_hrsold-hrsold)*60));
seconds=setzero(Math.floor((e_minsold-minsold)*60));
document.getElementById('days').innerHTML="已运行"+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";
}
function setzero(i){
if (i<10)
{i="0" + i};
return i;
}
show_date_time();
</script>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">94</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">20</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
                <a title="收藏到书签，偶尔High一下^_^" rel="alternate" class="mw-harlem_shake_slow wobble shake" href="javascript:(function() {
    function c() {
        var e = document.createElement(&quot;link&quot;);
        e.setAttribute(&quot;type&quot;, &quot;text/css&quot;);
        e.setAttribute(&quot;rel&quot;, &quot;stylesheet&quot;);
        e.setAttribute(&quot;href&quot;, f);
        e.setAttribute(&quot;class&quot;, l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement(&quot;div&quot;);
        e.setAttribute(&quot;class&quot;, a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement(&quot;audio&quot;);
        e.setAttribute(&quot;class&quot;, l);
        e.src = i;
        e.loop = false;
        e.addEventListener(&quot;canplay&quot;, function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener(&quot;ended&quot;, function() {
            N();
            h()
        }, true);
        e.innerHTML = &quot; <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>&quot;;
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += &quot; &quot; + s + &quot; &quot; + o
    }
 
    function T(e) {
        e.className += &quot; &quot; + s + &quot; &quot; + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp(&quot;\\b&quot; + s + &quot;\\b&quot;);
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, &quot;&quot;)
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = &quot;//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3&quot;;
    var s = &quot;mw-harlem_shake_me&quot;;
    var o = &quot;im_first&quot;;
    var u = [&quot;im_drunk&quot;, &quot;im_baked&quot;, &quot;im_trippin&quot;, &quot;im_blown&quot;];
    var a = &quot;mw-strobe_light&quot;;
    var f = &quot;//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css&quot;;
    var l = &quot;mw_added_css&quot;;
    var b = g();
    var w = y();
    var C = document.getElementsByTagName(&quot;*&quot;);
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn(&quot;Could not find a node of the right size. Please try a different page.&quot;);
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()"><i class="fa fa-music"></i> High</a>

            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/huangguangda" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:2397923107@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/c785ece603d1" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-android"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_36232611" target="_blank" title="csdn">
                      
                        <i class="fa fa-fw fa-spinner"></i>csdn</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.douban.com/people/186539024/" target="_blank" title="豆瓣">
                      
                        <i class="fa fa-fw fa-bath"></i>豆瓣</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/da-shu-xiao-sheng/activities" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-globe"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/5a16e1f3f265da43128096cb" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-google"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5507262458/profile?topnav=1&wvr=6" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzI1NTcxOTQ1Nw==&scene=124#wechat_redirect" target="_blank" title="历史">
                      
                        <i class="fa fa-fw fa-apple"></i>历史</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://www.cnblogs.com/dashucoding/" target="_blank" title="博客园">
                      
                        <i class="fa fa-fw fa-facebook"></i>博客园</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gdone.gitee.io/englishpages/index.html" target="_blank" title="英文简历">
                      
                        <i class="fa fa-fw fa-globe"></i>英文简历</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gdone.gitee.io/pages/" target="_blank" title="中文简历">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>中文简历</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/GDone/events" target="_blank" title="码云">
                      
                        <i class="fa fa-fw fa-twitter"></i>码云</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://my.oschina.net/xiaomaomi1997/" target="_blank" title="开源中国">
                      
                        <i class="fa fa-fw fa-facebook"></i>开源中国</a>
                  </span>
                
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
                <img src="/images/cc-by-nc-sa.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa fa-fw fa-globe"></i>
                推荐阅读
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.alloyteam.com/nav/" title="Web前端导航" target="_blank">Web前端导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.36zhen.com/t?id=3448" title="前端书籍资料" target="_blank">前端书籍资料</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ife.baidu.com/" title="百度前端技术学院" target="_blank">百度前端技术学院</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://wf.uisdc.com/cn/" title="google前端开发基础" target="_blank">google前端开发基础</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.geekbang.org/" title="极客邦科技" target="_blank">极客邦科技</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ilxdh.com/" title="龙轩导航" target="_blank">龙轩导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://code.giffox.com/" title="程序员导航" target="_blank">程序员导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://search.chongbuluo.com/" title="快搜" target="_blank">快搜</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.android-doc.com/training/index.html" title="Android中文API" target="_blank">Android中文API</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://developer.android.google.cn/" title="Android Developers" target="_blank">Android Developers</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.freecodecamp.cn/" title="FreeCodeCamp中文社区" target="_blank">FreeCodeCamp中文社区</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://docs.oracle.com/javase/specs/" title="Java SE规范" target="_blank">Java SE规范</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://liuwangshu.cn/" title="刘望舒" target="_blank">刘望舒</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.colabug.com/author/3159ee71c93b3b4a/" title="小楼昨夜又秋风" target="_blank">小楼昨夜又秋风</a>
                  </li>
                
              </ul>

            </div>
          


        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="heart">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">达叔小生</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">357.6k</span>
  
</div>






  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共357.6k字</span>
</div>

<div class="weixin-box">
  <div class="weixin-menu">
    <div class="weixin-hover">
      <div class="weixin-description">微信扫一扫，订阅本博客</div>
    </div>
  </div>
</div>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">本站访客数</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">本站总访问量</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>





  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=65809965";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  


  <script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script>
<script type="text/javascript" src="/js/src/custom.js"></script>


   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none"></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/src/fireworks.js"></script>


  <!-- 代码块复制功能 -->
  <script type="text/javascript" src="/js/src/clipboard.min.js"></script>  
  <script type="text/javascript" src="/js/src/clipboard-use.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"live2d-widget-model-hibiki"},"display":{"position":"right","width":75,"height":150},"mobile":{"show":false},"log":false});</script></body>
</html>